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
方便理解

  1. #形如:
  2. [ expression for item in list if conditional ]
  3. #形如:
  4. [ expression for item in list ]
  5. #构造列表-从0到10的平方:
  6. squares = []
  7. for x in range(11):
  8. squares.append(x**2)
  9. #递推式构造列表
  10. squares = [x**2 for x in range(11)]
  11. #普通方式:
  12. num = []
  13. for x in range(1,30):
  14. for y in range(x,30):
  15. for z in range(y,30):
  16. if x**2 + y**2 == z**2:
  17. num.append((x,y,z))
  18. #递推式构造列表改写:
  19. [(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]
  20. #递推式构造列表只有if没有else,可以通过其它方式实现:
  21. lst = [(i%2==0 and 'e') or 'o' for i in range(20)]
  22. #补充
  23. print(0%2==0 and 'e' or 'o')
  24. #输出 e
  25. print(1%2==0 and 'e' or 'o')
  26. #输出 o
  27. #递推式构造字典:
  28. import string
  29. dct = {c:ord(c) for c in string.ascii_lowercase}
  30. #输出如下:
  31. #{'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}