Python lambda 表达式
2021-07-22 09:55
标签:回文 ges lang exp ons 方式 UNC func 一个 lambda 定义匿名函数 参数: https://www.geeksforgeeks.org/python-lambda-anonymous-functions-filter-map-reduce/ Python lambda 表达式 标签:回文 ges lang exp ons 方式 UNC func 一个 原文地址:https://www.cnblogs.com/shix0909/p/15037559.html语法:
lambda arguments: expression
Example 1: lambnda 使用方式
def cube(y):
return y*y*y
lambda_cube = lambda y: y*y*y
Example 2: list 奇数
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
final_list = list(filter(lambda x: (x%2 != 0) , li))
print(final_list)
Example 3: list 求大于某个值
ages = [13, 90, 17, 59, 21, 60, 5]
adults = list(filter(lambda age: age>18, ages))
print(adults)
Example 4: 求两个数组的交集
def interSection(arr1,arr2):
result = list(filter(lambda x: x in arr1, arr2))
print ("Intersection : ",result)
if __name__ == "__main__":
arr1 = [1, 3, 4, 5, 7]
arr2 = [2, 3, 5, 6]
interSection(arr1,arr2)
Example 5: 求list 中是回文字符串
my_list = ["geeks", "geeg", "keek", "practice", "aa"]
result = list(filter(lambda x: (x == "".join(reversed(x))), my_list))
print(result)
文章标题:Python lambda 表达式
文章链接:http://soscw.com/index.php/essay/106835.html