如何将[i for i in range(20)]变成产生器
a = [i for i in range(10)] print(a) print(type(a)) # <class 'list'> b = (i for i in range(10)) print(b) print(type(b)) # <generator object <genexpr> at 0x0000017EFE3DA548> # <class 'generator'> for i in a: print(i) for i in b: print(i) x = (1,2,3,4) print(type(x)) # <class 'tuple'>
如果列表是通过for循环产生的,只需要将方括号变成圆括号,就会将列表变成一个产生器