1、lambda - filter reduce map
https://python-course.eu/advanced-python/lambda-filter-reduce-map.php
2、递推式构造列表
https://python-course.eu/advanced-python/list-comprehension.php
方便理解
#形如:
[ expression for item in list if conditional ]
#形如:
[ expression for item in list ]
#构造列表-从0到10的平方:
squares = []
for x in range(11):
squares.append(x**2)
#递推式构造列表
squares = [x**2 for x in range(11)]
#普通方式:
num = []
for x in range(1,30):
for y in range(x,30):
for z in range(y,30):
if x**2 + y**2 == z**2:
num.append((x,y,z))
#递推式构造列表改写:
[(x,y,z) for x in range(1,30) for y in range(x,30) for z in range(y,30) if x**2 + y**2 == z**2]
#递推式构造列表只有if没有else,可以通过其它方式实现:
lst = [(i%2==0 and 'e') or 'o' for i in range(20)]
#补充
print(0%2==0 and 'e' or 'o')
#输出 e
print(1%2==0 and 'e' or 'o')
#输出 o
#递推式构造字典:
import string
dct = {c:ord(c) for c in string.ascii_lowercase}
#输出如下:
#{'a': 97, 'b': 98, 'c': 99, 'd': 100, 'e': 101, 'f': 102, 'g': 103, 'h': 104, 'i': 105, 'j': 106, 'k': 107, 'l': 108, 'm': 109, 'n': 110, 'o': 111, 'p': 112, 'q': 113, 'r': 114, 's': 115, 't': 116, 'u': 117, 'v': 118, 'w': 119, 'x': 120, 'y': 121, 'z': 122}