Basic Python

lambda

Naranjito 2022. 11. 29. 14:05
  • lambda

It is usually used to concise a function in short.

add=lambda a,b:a+b
result=add(4,5)
>>>result
9

#same as below
def add(a,b):
    return a+b
>>>add(4,5)
9

 

1. filter

nums=[1,2,3,4,5]
>>>print(filter(lambda n:n%2==0,nums))
>>>print(list(filter(lambda n:n%2==0,nums)))

<filter object at 0x108e36040>
[2, 4]
print(list(filter(lambda n:len(n)>1,df_list_100)))
>>>
[['183.203.180.184', '80'], ['95.174.64.70', '80'], ['185.13.223.1', '80'],
...
str1="Winter Olympics in 2022 will take place in Beijing China"
print(list(filter(lambda x:True if x.lower() in 'aeiou' else False,str1)))
>>>
['i', 'e', 'O', 'i', 'i', 'i', 'a', 'e', 'a', 'e', 'i', 'e', 'i', 'i', 'i', 'a']

 

2. map

It returns new list.

nums=[1,2,3,4,5,6,7]
>>>print(map(lambda n:n%2==0, nums))
>>>print(list(map(lambda n:n%2==0, nums)))

<map object at 0x108e6eb50>
[False, True, False, True, False, True, False]

 

2-1. filter, map

lst1=[1000, 500, 600, 700, 5000, 90000, 17500]

>>>
list(map(lambda n:n+2000,filter(lambda x:x<8000, lst1)))
[3000, 2500, 2600, 2700, 7000]

Convert a number to positive if it's negative in the list. Only pass those that are converted from negative to positive to the new list.

lst1=[-1000, 500, -600, 700, 5000, -90000, -17500]
list(filter(lambda x:True if x>0 else False,(map(lambda x:x*-1, lst1))))
>>>
[1000, 600, 90000, 17500]

 

3. reduce

1) Apply a function (or callable) to the first two items in an iterable and generate a partial result.

2) Use that partial result, together with the third item in the iterable, to generate another partial result.

3) Repeat the process until the iterable is exhausted and then return a single cumulative value.

import functools
nums=[1,3,5,6,7]
>>>print(functools.reduce(lambda x,y:x+y, nums))
22
from functools import reduce

num=[1,2,3,4,5]
def my_add(a,b):
    result=a+b
    print(f'{a}+{b}={result}')
    return result
    
>>>reduce(my_add,num,100) #since I supply a value of 100 to initializer, reduce() uses that value in the first call as the first argument to my_add()
100+1=101
101+2=103
103+3=106
106+4=110
110+5=115
115

 

4. sorted

Sorts by ascending order or descending order.

lst=[100, 10, 10000, 1, 9, 999, 99]
sorted(lst,key=lambda x:x)
>>>
[1, 9, 10, 99, 100, 999, 10000]

sorted(lst,key=lambda x:100/x)
>>>
[10000, 999, 100, 99, 10, 9, 1]

 

5. max

arr = [(0,10),(1,14),(2,2),(3,24)]
str = max(arr,key = lambda x:x[1]) 

>>>

(3, 24)

Each element in the arr enters to Lambda as inputs in order.

x[0] is the first input of each element tuple.

x[1] is the second input of each element tuple.

For example, the first element of arr is (0,10), so x[0]=0,x[1]=10.

So, in this example is meaning that find the maximun x[1].

Since 24is the largest, the result is (3,24).

'Basic Python' 카테고리의 다른 글

assert  (0) 2022.12.05
return vs yield, yield vs yield from  (0) 2022.11.30
vars  (0) 2022.08.25
__new__, __init__  (0) 2022.07.13
Underscore(_)  (0) 2022.07.05