「Python入門」ジェネレータ(generator)を作成するサンプル

サンプルコード
list = [x for x in range(10)]
for x in enumerate(list):
print(x,end=’ ‘) # (0, 0) (1, 1) (2, 2) (3, 3) (4, 4) (5, 5) (6, 6) (7, 7) (8, 8) (9, 9)
print()
generator = (x for x in range(10))
for x in generator:
print(x,end=” “) # 0 1 2 3 4 5 6 7 8 9

Comments are closed.