[Tutor] Help with a loop

Andreas Kostyrka andreas at kostyrka.org
Tue Mar 18 17:05:19 CET 2008


Assuming that len(b) > len(a):

>>> zip(itertools.cycle(a), b)
[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e'), (6, 'f'), (1, 'g'), (2, 'h'), (3, 'i'), (4, 'j')]

Andreas


Am Dienstag, den 18.03.2008, 16:23 +0100 schrieb PyProg PyProg:
> Hello,
> 
> I have a little problem, I have two lists:
> 
> >>> a=[1, 2, 3, 4, 5, 6]
> >>> b=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
> 
> ... and I want to obtain:
> 
> >>> [('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6), ('g',
> 1), ('h', 2), ('i', 3), ('j', 4)]
> 
> I want to obtain that with a comprehension-list.
> 
> Lists can have dimension completely different.
> 
> Can you help me please ?.
> 
> a+
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://mail.python.org/pipermail/tutor/attachments/20080318/94af11a0/attachment.pgp 


More information about the Tutor mailing list