I love this [Tutor] list. There are always new tricks that change the way I write code. And it makes me like Python more every day.
I keep a script file with "notes" on the things I learn here and I refer to these notes frequently. Here are the notes I made for this thread: """ iterate/map/modulus/(zip) """ a = (1, 2, 3, 4, 5) b = ('r', 'g', 'b') na = len(a) nb = len(b) print print "===================================" # zip A = ''.join([str(i) for i in a]) # "12345" B = ''.join(b) # "rgb" print zip(A, B), " - zip (inadequate)" print print "===================================" # brute force m = 0 n = 0 t = [] ##x = 10 ##for i in range(x): for i in range(max(na, nb)): if m == na: m = 0 if n == nb: n = 0 t.append((a[m], b[n])) m += 1 n += 1 print t, "- brute force" print print "===================================" # itertools from itertools import izip, cycle print list(izip(a, cycle(b))), "- itertools" print print "===================================" # map print map(None,a,b*(na/nb+1))[:na], "- map" print print "===================================" # modulus print [(a[i], b[i%nb]) for i in range(na)], "- modulus" print print "-----------------------------------" ##x = max(na, nb) x = 10 print [(a[i%na], b[i%nb]) for i in range(x)], "- modulus (extended)" print print "===================================" This mailing list is great. Thanks to all the experienced Python coders who offer various solutions to the questions, and to all the beginners who ask them. -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Reed O'Brien Sent: Sunday, June 17, 2007 8:59 AM To: Iyer; tutor@python.org Subject: Re: [Tutor] iterating over a sequence question.. On Jun 17, 2007, at 3:44 AM, John Fouhy wrote: > On 17/06/07, Iyer <[EMAIL PROTECTED]> wrote: >> >> say, if I have a list l = [1,2,3,5] >> >> and another tuple t = ('r', 'g', 'b') >> >> Suppose I iterate over list l, and t at the same time, if I use >> the zip >> function as in zip(l,t) , I will not be able to cover elements 3 >> and 5 in >> list l >> >>>>> l = [1,2,3,5] >>>>> t = ('r', 'g', 'b') >>>>> for i in zip(l,t): >> ... print i >> ... >> (1, 'r') >> (2, 'g') >> (3, 'b') >> >> is there an elegant way in python to print all the elements in l, >> while >> looping over list t, if len(t) != len(l) as to get the output: >> >> (1, 'r') >> (2, 'g') >> (3, 'b') >> (5, 'r') > > Check out the itertools module. I don't have the ability to test this > right now, but try something like: > > import itertools > lst = [1,2,3,5] > t = ('r', 'g', 'b') > > itertools.izip(lst, itertools.cycle(t)) > > -- > John. > +1 for John's solution usage: >>> [x for x in itertools.izip(lst, itertools.cycle(t)] >>> [(1, 'r'), (2, 'g'), (3, 'b'), (5, 'r')] _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor