lambda argument: manipulate(argument)
add = lambda x, y: x + y
print(add(3, 5))
# Output: 8
list.sort(key=..., reverse=...)#sort函数在原list进行排序
sorted(list, key=..., reverse=...)#sorted函数返回一个新的list
# take second element for sort
def takeSecond(elem):
return elem[1]
# random list
random = [(2, 2), (3, 4), (4, 1), (1, 3)]
# sort list with key
random.sort(key=takeSecond)
# print list
print('Sorted list:', random)
# output
Sorted list: [(4, 1), (2, 2), (1, 3), (3, 4)]
a = [(1, 2), (4, 1), (9, 10), (13, -3)]
a.sort(key=lambda x: x[1])
print(a)
# Output: [(13, -3), (4, 1), (1, 2), (9, 10)]
这里传入lambda表达式形参x的实参为列表a中的每一个元素