Yielding from sorted iterators in sorted order in Python?

前端 未结 1 1181
孤独总比滥情好
孤独总比滥情好 2021-01-02 00:45

Is there a better way to merge/collate a bunch of sorted iterators into one so that it yields the items in sorted order? I think the code below works but I feel like there i

相关标签:
1条回答
  • 2021-01-02 01:07

    yes, you want heapq.merge() which does exactly one thing; iterate over sorted iterators in order

    def sortkey(row):
        return (row[5], row)
    
    def unwrap(key):
        sortkey, row = key
        return row
    
    from itertools import imap
    FILE_LIST = map(file, ['foo.csv', 'bar.csv'])
    input_iters = imap(sortkey, map(csv.csvreader, FILE_LIST))
    output_iter = imap(unwrap, heapq.merge(*input_iters))
    
    0 讨论(0)
提交回复
热议问题