推导式
特性
- 好好应用推导式后面的条件判断
列表推导式
list1 = [i for i in range(10) if not(i%2) and i % 3]
list1 = [2, 4, 8]
字典推导式
dict1 = {i:i % 2==0 for i in range(10)}
dict1 = {0: True, 1: False, 2: True, 3: False, 4: True, 5: False, 6: True, 7: False, 8: True, 9: False}
集合推导式
set1 = {i for i in [1,1,2,2,3,4,5,6,7,8]}
set1 = {1,2,3,4,5,6,7,8}
生成器推导式
d = (i for i in range(100))
可以用next迭代
也可以用for迭代
for each in d:
print(each)