a为生成器,b为列表

1 #_*_coding:utf-8_*_
2 #__author__ = "csy"
3 
4 a = (i for i in range(10000000))
5 b = [i for i in range(10000000)]
6 
7 print(b[1000])
8 print(a[1000])

输出:

1000

File "C:/Users/...../PycharmProjects/1/4.py", line 7, in <module>
print(a[1000])
TypeError: 'generator' object is not subscriptable

b能直接打印第1000个元素,a不行。a要在真正循环调用时才生产

posted on 2017-09-09 19:39  csy113  阅读(106)  评论(0编辑  收藏  举报