(1)列表推导式和(2)生成器表达式
(1)和(2)的区别是一个是一起再内存中全部生成,一个是用到的时候在生成
>>> [i for i in range(10)] 标致是[]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> (i for i in range(10)) 标致是()
<generator object <genexpr> at 0x01607418>
>>> {i for i in range(10)}
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> {1,1+1 for i in range(10)}
SyntaxError: invalid syntax
>>> {i for i in range(10)}
set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> {i+1 for i in range(10)}
set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
>>> {i:i+1 for i in range(10)}
{0: 1, 1: 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10}
>>> {i;i+1 for i in range(10)}
SyntaxError: invalid syntax