【发布时间】:2014-01-30 12:02:01
【问题描述】:
如果输入是
round_robin(range(5), "hello")
我需要输出为
[0, 'h', 1, 'e', 2, 'l', 3, 'l', 4, 'o']
我试过了
def round_robin(*seqs):
list1=[]
length=len(seqs)
list1= cycle(iter(items).__name__ for items in seqs)
while length:
try:
for x in list1:
yield x
except StopIteration:
length -= 1
pass
但它给出了错误
AttributeError: 'listiterator' object has no attribute '__name__'
如何修改代码以获得想要的输出?
【问题讨论】:
-
[ i for t in zip(range(5), "hello") for i in t] -
roundrobinrecipe 已经存在于 itertools 的文档中:docs.python.org/2/library/itertools.html#recipes
标签: python python-2.7