1、lambda,map
1 num_l=[1,2,3,4,5,6] 2 string='fuhaoka' 3 a=map(lambda x:x+1,num_l) 4 print(list(a)) 5 b=map(lambda x:x.upper(),string) 6 print(list(b)) 7 c=map(lambda x:x**0.5,num_l) 8 print(list(c)) 9 10 11 12 >>> 13 [2, 3, 4, 5, 6, 7] 14 ['F', 'U', 'H', 'A', 'O', 'K', 'A'] 15 [1.0, 1.4142135623730951, 1.7320508075688772, 2.0, 2.23606797749979, 2.449489742783178]
2、filter
1 student=['wang1','wang2','zhang1','zhang2','wang3','wang4','ma1','ma2'] 2 3 # def wang_xing(arry): 4 # wang=[] 5 # for i in arry: 6 # if not i.startswith('wang'):#不是以'wang开头' 7 # wang.append(i) 8 # return wang 9 # print(wang_xing(student)) 10 11 12 a=filter(lambda x:x.startswith('wang'),student) 13 print(list(a)) 14 15 16 17 >>> 18 ['wang1', 'wang2', 'wang3', 'wang4']
3、filter 取反
1 student=['wang1','wang2','zhang1','zhang2','wang3','wang4','ma1','ma2'] 2 a=list(filter(lambda x: not x.endswith("2"),student)) #取出最后不是2的元素 3 print("不是以2结尾的元素为:",a) 4 5 b=list(filter(lambda x: x.endswith("2"),student)) #取出最后是2的元素 6 print("以2结尾的元素为:",b) 7 8 9 >>> 10 不是以2结尾的元素为: ['wang1', 'zhang1', 'wang3', 'wang4', 'ma1'] 11 以2结尾的元素为: ['wang2', 'zhang2', 'ma2']