Merging multiple sorted sequences.

Erik python at lucidity.plus.com
Wed Apr 12 19:09:42 EDT 2017


On 12/04/17 23:44, Ian Kelly wrote:
> This might be okay since Timsort on an already-sorted list should be
> O(n). But there's not really any need to keep them sorted and I would
> just use "lowest = min(items, key=itemgetter(0))".

Sure (and this was my main worry). I had it in my head for some reason 
that min() would return the smallest key, not the object (and hence I 
wouldn't be able to know which sequence object to get the next value 
from - sorting means it's always at index 0).

But of course, min() returns the outer object containing the key so I 
don't need to sort them to know how to address the correct sequence object.

operator.itemgetter() probably helps a bit, too ;)

I've done all that and it works.

Thanks, E.



More information about the Python-list mailing list