美文网首页
lambda map filter  reduce sorted

lambda map filter  reduce sorted

作者: 程序里的小仙女 | 来源:发表于2021-08-26 22:01 被阅读0次

my_list = [3, 1, 5, 4, 10]

# 元素全加1,结果:[4, 2, 6, 5, 11]

list(map(lambda i:i+1, my_list))

# 过滤小于10的元素,结果:[3, 1, 5, 4]

list(filter(lambda i:i<10, my_list))

# 元素累加,结果:33

from functools import reduce

reduce(lambda i,j:i+j, my_list, 10)

# 字典按值排序,结果:[( b , 1), ( a , 3), ( d , 4), ( c , 5)]

my_dict = { a :3,  b :1,  c :5,  d :4}

sorted(my_dict.items(), key=lambda item:item[1])

相关文章

网友评论

      本文标题:lambda map filter  reduce sorted

      本文链接:https://www.haomeiwen.com/subject/khcpiltx.html