[英]how to loop infinite times by repeating same elements with single list in python
I am trying this and getting Memory error. 我正在尝试这样做,并遇到内存错误。
A = [a,b,c,d]
for i,j in zip(cycle(A), itertools.count()):
print (i+str(j))
how can i print this output: 我如何打印此输出:
[a1, b1, c1, d1, a2, b2, c2, d2, a3, b3, c3, d3 .. infinite times]
You're on Python 2.7, and zip
tries to create a list from an inexhaustible iterator; 您使用的是Python 2.7,而zip
试图通过一个取之不尽的迭代器创建一个列表。 your memory simply blows up. 你的记忆力简直炸毁了。 Use itertools.izip
instead. 请改用itertools.izip
。
Here is how you could proceed: ( I added a break from the loop after 10000, you can remove it to get to infinity and beyond) 这是您可以继续的方法:(我在10000之后在循环中添加了一个中断,可以将其删除以达到无穷远甚至更远)
import itertools
A = ['a', 'b', 'c', 'd']
for idx, letter in enumerate (itertools.cycle(A)):
print(letter + str(idx), end=' ')
if idx == 10000:
break
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.