Here are 10 essential Python functions that every data scientist should know to work efficiently with data: 1. len() Returns the length (number of elements) of an object like a list, tuple, or string. data = [1, 2, 3, 4] print (len( data )) # Output : 4 2. type() Returns the type of the given object. Useful for checking data types. x = 42 print ( type (x)) # Output : < class ' int '> 3. map() Applies a function to every item in an iterable (like a list or tuple). numbers = [ 1 , 2 , 3 , 4 ] squared = list ( map (lambda x: x ** 2 , numbers)) print (squared) # Output: [1, 4, 9, 16] 4. filter() Filters elements in an iterable based on a function that returns True or False . numbers = [ 1 , 2 , 3 , 4 , 5 ] even = list ( filter (lambda x: x % 2 == 0 , numbers)) print (even) # Output: [2, 4] 5. reduce() (from functools ) Applies a function cumulatively to the items of an iterable, reducing it to a single value. from functools import reduce n...
댓글
댓글 쓰기