1 列表推导式: 2 3 >>> a = [i for i in range(10) if i % 2 == 0] 4 >>> a 5 [0, 2, 4, 6, 8] 6 7 字典推导式: 8 9 >>> b = {i:i % 2 == 0 for i in range(10)} 10 >>> b 11 {0: True, 1: False, 2: True, 3: False, 4: True, 5: False, 6: True, 7: False, 8: True, 9: False} 12 13 集合推导式: 14 15 >>> c = {i for i in [1,2,1,1,1,4,3,3,4,4,5]} 16 >>> c 17 {1, 2, 3, 4, 5} 18 19 没有元组推导式,用元组的规则其实生成的是生成器推导式 20 21 >>> d = (i for i in range(10)) 22 >>> d 23 <generator object <genexpr> at 0x029B6930> 24 >>> for each in d: 25 print(each,end=' ') 26 27 0 1 2 3 4 5 6 7 8 9