将列表变成产生器
- 使用圆括号(),然后括号内需要使用for循环
a = [i for i in range(5)]
print(a)
print(type(a))
b = (i for i in range(5)) #圆括号()就是将列表变成了产生器,并不是变成了元组类型
print(type(b))
for i in b:
print(i)

总结
1.如果列表是通过for循环产生的,只需要将方括号[]变成圆括号(),就可以将列表变成一个产生器使用
加油 2020-2-26
网友评论