借助lambda

  • filter
1
2
3
4
5
6
7
8
def foo(a, b, c):
return a < b and b < c

myTuple = (1, 2, 3, 4, 5, 6)

# 找到大于1,小于4的值
print(list(filter(lambda x: foo(1, x, 4), myTuple)))
# (2, 3)
  • map
1
2
3
4
5
6
7
8
9
10
def func(x, y):
print(x, y)
return x + y

a = [1, 2]
data = list(map(lambda x: func(x, 88), a))
print(data)
# 1 88
# 2 8
# [89, 90]