On Apr 1, 2005, at 09:59, Alan Gauld wrote:
Since the data are obviously related (since you need to keep them linked), I'd be inclined to merge the lists into a list of tuples
merged = [(a,b,c,d) for a in l1 for b in l2 for c in l3 for d in l4]
Then you can sort 'merged' and it should just work.... The default sort will order the tuples by the first member...
Alan G.
What's the advantage of this compared to the "zip" method (which yields the same result AFAICT)?
Yikes! Alan must have been up too late. They are not the same at all. Alan's code creates a list containing *every combination* of one element from each source list:
>>> a=[1,2,3]
>>> b=[4,5,6]
>>> c=[7,8,9]
>>> [ (aa,bb,cc) for aa in a for bb in b for cc in c ]
[(1, 4, 7), (1, 4, 8), (1, 4, 9), (1, 5, 7), (1, 5, 8), (1, 5, 9), (1, 6, 7), (1, 6, 8), (1, 6, 9), (2, 4, 7), (2, 4, 8), (2, 4, 9), (2, 5, 7), (2, 5,
8), (2, 5, 9), (2, 6, 7), (2, 6, 8), (2, 6, 9), (3, 4, 7), (3, 4, 8), (3, 4, 9), (3, 5, 7), (3, 5, 8), (3, 5, 9), (3, 6, 7), (3, 6, 8), (3, 6, 9)]
This is very different from zip(a,b,c) which makes a list of *corresponding* items from each list:
>>> zip(a,b,c) [(1, 4, 7), (2, 5, 8), (3, 6, 9)]
Kent
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor