Python---匿名函数

Python从入门到高手(内部资源-配学习资料)_哔哩哔哩_bilibili

# map
list1 = [3, 4, 5, 2, 7, 6, 8, 9, 3]
result = map(lambda x: x + 2, list1)
print(result)
print(list(result))

func = lambda x: x if x % 2 == 0 else x + 1
result = func(5)
print(result)

# 对列表中的奇数进行加1操作
result = map(lambda x: x if x % 2 == 0 else x + 1, list1)
print(list(result))  # [4, 4, 6, 2, 8, 6, 8, 10, 4]

# reduce(): 对列表中的元素进行加减乘除运算的函数
from functools import reduce

tuple1 = (3, 5, 7, 8, 9)

result = reduce(lambda x, y: x + y, tuple1)

print(result)

tuple2 = (1,)
result = reduce(lambda x, y: x + y, tuple2, 10)
print(result)  # 11

tuple2 = (1,)
result = reduce(lambda x, y: x - y, tuple2, 10)
print(result)  # 9
#################################################
list1 = [12, 6, 8, 9, 64, 56, 2, 8, 0]
result = filter(lambda x: x > 10, list1)  # [12, 64, 56]
print(list(result))

students = [{'name': 'lily', 'age': 13}, {'name': 'steven', 'age': 21}, {'name': 'lucy', 'age': 19}]
result = filter(lambda x: x['age'] > 20, students)
print(list(result))

# 按照年龄从小到大排序
students = sorted(students, key=lambda x: x['age'], reverse=True)
print(students)

'''
max()
min()
sorted()
map()
reduce()
filter()
'''
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值