Merging multiple sorted sequences.
python at lucidity.plus.com
Wed Apr 12 20:39:19 EDT 2017
On 13/04/17 00:09, Erik wrote:
> On 12/04/17 23:44, Ian Kelly wrote:
>> I would
>> just use "lowest = min(items, key=itemgetter(0))".
> 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).
Actually, no, that wasn't my issue. It was that I don't know the index
of the source to 'del'ete from the list when it has exhausted.
The code always deletes item 0 and my test sequences are such that they
happen to exhaust in first to last order, so that works by accident. If
you swap the definitions of the 'a', 'b', and 'c' test data, then it all
goes horribly wrong with the "min()" change.
However, I can use the 'remove' method on the items list to delete that
element instead. It's going to do a linear search of the list to find
the correct element, but I expect the number of sources to be trivially
small compared to the amount of data in each, so for my specific case
that should be OK.
More information about the Python-list