2012-07-18 2 views

답변

7

봐 :

>>> from itertools import cycle, islice 
>>> def roundrobin(*iterables): 
     "roundrobin('ABC', 'D', 'EF') --> A D E B F C" 
     # Recipe credited to George Sakkis 
     pending = len(iterables) 
     nexts = cycle(iter(it).next for it in iterables) 
     while pending: 
      try: 
       for next in nexts: 
        yield next() 
      except StopIteration: 
       pending -= 1 
       nexts = cycle(islice(nexts, pending)) 


>>> for x in roundrobin("ABCD", "12"): 
     print x 


A 
1 
B 
2 
C 
D 
관련 문제