1.lambda函数:又称匿名函数,示例如下:
def f(x):
return x**2
print f(4) #16
g = lambda x:x**2
print g(4) #16
2.map函数
print map(lambda x:x**2,range(10))
#[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
3.sorted函数
dict = {9:2,4:3,6:9,‘a‘:‘test‘,‘e‘:‘fff‘,‘*‘:‘$‘}
print sorted(dict.items(),key = lambda x:x[0])
#[(4, 3), (6, 9), (9, 2), (‘*‘, ‘$‘), (‘a‘, ‘test‘), (‘e‘, ‘fff‘)] 按照key值排序
print sorted(dict.items(),key = lambda x:x[1])
#[(9, 2), (4, 3), (6, 9), (‘*‘, ‘$‘), (‘e‘, ‘fff‘), (‘a‘, ‘test‘)] 按照vlue值排序
时间: 2024-12-28 16:24:08