[Tutor] Iterable Understanding

Stephen Nelson-Smith sanelson at gmail.com
Sat Nov 14 15:49:58 CET 2009


Hi Wayne,

> Just write your own merge:
> (simplified and probably inefficient and first thing off the top of my head)
> newlist = []
> for x, y, z in zip(list1, list2, list3):

I think I need something like izip_longest don't I, since the list wil
be of varied length?

Also, where do these lists come from?  They can't go in memory -
they're much too big.  This is why I felt using some kind if generator
was the right way - I can produce 3 (or 12) sets of tuples... i just
need to work out how to merge them.

>     if y > x < z:
>         newlist.append(x)
>     elif x > y < z:
>         newlist.append(y)
>     elif x > z < y:
>         newlist.append(z)
> I'm pretty sure that should work although it's untested.

Well, no it won't work.  The lists are in time order, but they won't
match up.  One log may have entries at the same numerical position (ie
the 10th log entry) but earlier than the entries on the previous
lines.  To give a simple example:

List 1        List 2        List 3
(1, cat)      (2, fish)     (1, cabbage)
(4, dog)     (5, pig)      (2, ferret)
(5, phone)  (6, horse)  (3, sausage)

Won't this result in the lowest number *per row* being added to the
new list?  Or am I misunderstanding how it works?

S.


More information about the Tutor mailing list