Best way to sort 1M records in Python

前端 未结 11 1637
慢半拍i
慢半拍i 2020-12-05 08:41

I have a service that runs that takes a list of about 1,000,000 dictionaries and does the following

myHashTable = {}
myLists = { \'hits\':{}, \'misses\':{},          


        
11条回答
  •  臣服心动
    2020-12-05 09:32

    Instead of trying to keep your list ordered, maybe you can get by with a heap queue. It lets you push any item, keeping the 'smallest' one at h[0], and popping this item (and 'bubbling' the next smallest) is an O(nlogn) operation.

    so, just ask yourself:

    • do i need the whole list ordered all the time? : use an ordered structure (like Zope's BTree package, as mentioned by Ealdwulf)

    • or the whole list ordered but only after a day's work of random insertions?: use sort like you're doing, or like S.Lott's answer

    • or just a few 'smallest' items at any moment? : use heapq

提交回复
热议问题