生成器:我實現(xiàn)了這樣一個功能你試試
def count(n):
while True:
yield n
n += 1
c = count(0)
列表: 這個簡單。
def count(n):
haha = []
while True:
haha.append(n)
n += 1
return haha
c = count(0)
都準備好了,點火 開炮
完成后我們來取一下90~100的元素
- 生成器:ok啦
print(c[90:100])
# 輸出
TypeError: 'generator' object is not subscriptable
# 正確姿勢
import itertools
for i in itertools.islice(c, 90, 100):
print(i)
# 輸出
90
91
92
93
94
95
96
97
98
99
- 列表
蛋定,慢~來
靠,死機了