・ I posted the Lambda expression code to qiita the day before, but it violates PEP8 and the lambda expression works. I thought it wouldn't lead to self-growth if I seemed to confirm it. While understanding how to use the map function and filter function that are often rolled on the net, I felt the value of the lambda expression.
[Python] How do you use lambda expressions? ?? [Scribbles] [First post] https://qiita.com/sho_cullni/items/aba9a09b30abb20cfd1a
A function that executes function processing on a list and outputs a return value
map(function,List etc.)
I made a code that triples for a given list
map_test.py
num_list = [-4, -3, -2, -1, 0, 1, 2, 3, 4]
triple_num_list = map(lambda x: x * 3, num_list)
print(list(triple_num_list))
If you want to reproduce the above code using def, for example:
def triple(x):
x = x * 3
return x
num_list = [-4, -3, -2, -1, 0, 1, 2, 3, 4]
print(list(map(triple,num_list)))
The output result when the above two codes are executed is as follows
[-12, -9, -6, -3, 0, 3, 6, 9, 12]
A function that executes a function on a list, etc., and outputs only those that meet the conditions (becomes True) as return values.
filter(function,List etc.)
I made a code that retrieves multiples of 3 for a given list (A feeling of Nabeatsu in the world)
filter_test.py
num_list = [1, 2, 3, 4, 5, 6]
aho = filter(lambda x: x % 3 == 0, num_list)
print(list(aho))
If you want to reproduce the above code using def, for example:
def ahoaho(x):
for i in x:
if i % 3 == 0:
aho.append(i)
aho = []
num_list = [1, 2, 3, 4, 5, 6]
ahoaho(num_list)
print(list(aho))
The output result when the above two codes are executed is as follows
[3, 6]
-Sure, it seems easier to code than defining each function with def. .. .. !! -The combination technique of filter function and lambda expression can be used for web scraping. If you can understand it in a good way in the future, I will list it on qiita.
I have commented, so I will add it! Thank you for your comment every time! (^^) / I hope to update in the future what happens when I write in def ...!
Also, to be exact, it is not a lambda function, but a lambda expression. Excuse me. I have fixed it!
・ You can do the following with the following grammars sorted function: Sorts in ascending or descending order according to the result of the function. max function: Extracts the largest data for the result of the function. min function: Extracts the smallest data for the result of the function.
sorted(List etc.,function)・ ・ ・ Max and min are the same, so it may be good to remember them together!
・ It may not be a good subject, but the number of people infected with coronavirus is easy to imagine. Try sorting, listing the prefectures with the most infections, and vice versa.
sample.py
corona_infected = [["Tokyo", 11611],["Saitama", 2142],["Chiba",1512],["kanagawa", 2283]]
#Code example of sorted function, max function, min function
corona = sorted(corona_infected, key=lambda x: x[1])
corona_max = max(corona_infected,key=lambda x: x[1])
corona_min = min(corona_infected,key=lambda x: x[1])
#Output to console
print(corona_infected)
print(corona)
print(corona_max)
print(corona_min)
The output result when the above two codes are executed is as follows
[['Tokyo', 11611], ['Saitama', 2142], ['Chiba', 1512], ['kanagawa', 2283]]
[['Chiba', 1512], ['Saitama', 2142], ['kanagawa', 2283], ['Tokyo', 11611]]
['Tokyo', 11611]
['Chiba', 1512]
Recommended Posts