问题:
我有下面的代码。我想知道为什么我是否在下面的代码中包含带有注释的行并不重要。
#!/usr/bin/env python
from itertools import *
import time
cc = cycle([ iter([1,2,3]), iter([4]) , iter([5,6]) ] )
p = 3
while p:
try:
for k in cc:
print k.next()
except StopIteration:
p = p - 1
cc = cycle(islice(cc, p)) # this does not matter
输出:
1
4
5
2
6
3
也结帐roundrobin
食谱在
http://docs.python.org/2.7/library/itertools.html
此代码显示islice
正在影响cc
#!/usr/bin/env python
from itertools import *
import time
cc = cycle([ iter([1,2,3]), iter([4]) , iter([5,6]) ] )
p = 3
while p:
try:
for k in cc:
print k,k.next()
except StopIteration:
print "stop iter"
p = p - 1
cc = cycle(islice(cc, p))
输出
<listiterator object at 0x7f32bc50cfd0> 1
<listiterator object at 0x7f32bc518050> 4
<listiterator object at 0x7f32bc518090> 5
<listiterator object at 0x7f32bc50cfd0> 2
<listiterator object at 0x7f32bc518050> stop iter
<listiterator object at 0x7f32bc518090> 6
<listiterator object at 0x7f32bc50cfd0> 3
<listiterator object at 0x7f32bc518090> stop iter
<listiterator object at 0x7f32bc50cfd0> stop iter