1、推导列表
ans = [ x for x in range(6)]
print(ans)
ans = [ x for x in range(6) if x > 2]
print(ans)
ans = [ x*y for x in range(6) if x > 2 for y in range(6) if y < 3]
print(ans)
2、map 函数
a = list(map(list,"abc"))
print(a)
b = list(map(chr,[65,66,67,68]))
print(b)
c = list(map(str,(1,2,3)))
print(c)
def upper_letter(l):
if "a"<=l<="z":
l=l.upper()
return l
d = list(map(upper_letter,["a","b","c"]))
print(d)
e = list(map(lambda x:x*2,[1,2,3,4]))
print(e)
f = list(map(lambda x,y:x+y,[1,2,3,4],[5,6,7,8]))
print(f)
g = sum(list(map(lambda x:len(x),"I am a boy".split())))
print(g)
3、 filter函数
print("I am a good boy".split())
a = list(filter(lambda x:len(x)>=3,"I am a good boy".split()))
print(a)
a = [ i for i in "I am a good boy".split() if len(i) >=3]
print(a)
b = list(filter(lambda x:x>5,list(range(10))))
print(b)
def is_odd(n):
return n%2 == 1
c = list(filter(is_odd,list(range(10))))
print(c)
4、reduce
def add(x,y):
return x+y
# 操作是第一个值和第二值执行function操作,结果再与第三个值进行function操作
from functools import reduce
a = reduce(add,[1,2,3,4,5])
print(a)
b = reduce(lambda x,y:x*y,[1,2,3,4,5])
print(b)
5、lambda 函数
add = lambda x,y:x+y
print(add(1,2))