一、python 三元表达式
x = 1
y = 2
r = x if x < y else y
print("r=",r)
Python 内建函数主要分为:filter() map() reduce() zip()
1.filter
a = [1,2,3,4,5,6,7] print(list(filter(lambda x:x<5,a)))
2.map
a = [1,2,3,4,5,6,7] print(map(lambda x:x,a)) print(list(map(lambda x:x,a))) print(list(map(lambda x:x*x,a)))
a = [1,2,3,4,5,6,7] b = [1,1,1,1,1,1,1] print(list(map(lambda x,y:x+y,a,b)))
list_x = [1,2,3,4,5,6,7,8]
def squre(x):
return x*x
#不采用map 的方式
#########################################
list_xx=[]
for v in list_x:
list_xx.append(squre(v))
for v in list_xx:
print(v,end=' ')
print('\n')
#######################################
#采用map的方式进行
list_xx = list(map(squre,list_x))
print(list_xx)
reduce
from functools import reduce print(reduce(lambda x,y:x+y,[2,3,4],1)) # (1+2)+3+4
zip
zip((1,2,3),(4,5,6)) for i in zip((1,2,3),(4,5,6)): print(i) dicta = {'a':'aa','b':'bb'} dictb = zip(dicta.values(),dicta.keys()) print(dict(dictb))