In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to create python anonymous functions". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to create python anonymous functions.
Preface
When defining a function, you don't want to give the function a name. At this point, you can use lambda to define an anonymous function.
Syntax:
Variable name = lambda parameter: expression (block)
Parameters: optional, usually in the form of comma-separated variable expressions, that is, positional parameters
Expression: cannot contain loops, return, can contain if... Else...
Note:
1. Expressions cannot contain loops, return. Can include if... Else... Sentence.
two。 Parameters are optional. If there are multiple parameters, separate them with commas.
3. The result of expression evaluation is returned directly.
First, create an anonymous function: func=lambda: 3 > 2ret = func () # call the anonymous function, variable name (), and use the variable ret to receive the content returned by the expression print (ret) # Truedef func (): return 3 > 2ret = func () print (ret) # True# and func=lambda: 3 > 2 are equivalent
Running result:
TrueTrue
Create an anonymous function def func (x, y): return x + yret = func (1,2) print (ret) # anonymous function func = lambda x, y: X + yret = func (1,2) # if the anonymous function has parameters, you need to pass arguments when calling the anonymous function. Print (ret) # 3. Find the largest of the two numbers def func (x, y): if x
< y: return y else: return xret = func(30, 20)print(ret) #30#匿名函数func = lambda x, y: y if x < y else x# 注意if和else 没有 : 不能换行,if判断体写在判断条件前面ret = func(11, 2)print(ret) #11func = lambda x, y: y if x < y else x4. Exercises:
1. Change the add () method to an anonymous function
Def add (x, y): return x + y func = lambda x, y: X + yret = func (1,2) print (ret) # 3
two。 Use the max function to find the maximum value of the dictionary
Dic = {'K1: 10,'K2: 200,'K3: 20} ret = max (dic) # the default comparison is the size of the dictionary's key. ASCII for decimal print (ret) # k3def func (key): return [key] ret = max (dic, key=func) # change the comparison rule, compare the size with value, and the final function returned is the dictionary key print (ret) # K2 using the anonymous function ret = max (dic, key=lambda x: dic [x]) print (ret) # K2
3. Compare according to the valuez value of the dictionary 'name' in the list.
Lst = [{'name':' egon', 'price': 100}, {' name': 'rdw',' price': 666}, {'name':' zat', 'price': 1}] # ret = max (lst) # error and cannot be compared between dictionaries. # print (ret) def max_name (dic): return dic ['name'] # returns the value corresponding to the dictionary name ret = max (lst, key=max_name) # specifies that the comparison content print (ret) uses an anonymous function to compare: ret = max (lst, key=lambda dic: dic [' name']) # specify the comparison content print (ret)
Running result:
{'name':' zat', 'price': 1} {' name': 'zat',' price': 1}
Similarly, you can compare according to the valuez value corresponding to the dictionary 'price'' in the list.
Lst = [{'name':' egon', 'price': 100}, {' name': 'rdw',' price': 666}, {'name':' zat', 'price': 1}] def max_price (dic): return dic [' price'] # returns the value of the dictionary price anonymous function: ret = max (lst Key=lambda dic: dic ['price']) # specify comparison content 100 name': 666 ret 1 print (ret) # {' name': 'rdw',' price': 666}
4. Find the square of each element in lst
Lst = [1, 3, 4] def square (num): num2 = num * num return num2ret = map (square, lst) # iterator 1 i in ret 4 i in ret: print (I) # 1 i in ret 9 16 Anonymous function: ret = map (lambda num: num * num, lst) for i in ret: print (I) # 1je 9J 16
5. Filter out all odd numbers in the list:
Lst = [1,2,3,4,5,6,7,8,9,10] def odd (num): if num% 2 = = 1: return True else: return Falseret = filter (odd, lst) print (list (ret)) # [1,3,5,7,9] # Anonymous function: ret = filter (lambda num: True if num% 2 = = 1 else False, lst) print (list (ret)) # [1,3,5,7,9]
6. There are two tuples (('a'), ('b')), (('c'), ('d')). Please use the anonymous function in python to generate a list [{'a', ('b')]
Tup1 = (('a'), ('b')) tup2 = (('c'), ('d')) # [{'afid'}, {'baud')] ret = zip (tup1, tup2) # (('axie d`), (' baked d')) def func (tup): # ('aqu') ('baked d')) (' baud') return {tup [0], tup [1]} ret1=map (func) Ret) print (list (ret1)) # [{'aqure:' c'}, {'baked:' d'}] # Anonymous function: ret2 = list (map (lambda tup: {tup [0]: tup [1]}, ret)) print (ret2) # [{'aquired:' c'}, {'baked:' d'}]
At this point, I believe you have a deeper understanding of "how to create python anonymous functions". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.