Best way to sort 1M records in Python

前端 未结 11 1651
慢半拍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:17

    This seems to be pretty fast.

    raw= [ {'id':'id1', 'hits':200, 'misses':300, 'total':400},
        {'id':'id2', 'hits':300, 'misses':100, 'total':500},
        {'id':'id3', 'hits':100, 'misses':400, 'total':600}
    ]
    
    hits= [ (r['hits'],r['id']) for r in raw ]
    hits.sort()
    
    misses = [ (r['misses'],r['id']) for r in raw ]
    misses.sort()
    
    total = [ (r['total'],r['id']) for r in raw ]
    total.sort()
    

    Yes, it makes three passes through the raw data. I think it's faster than pulling out the data in one pass.

提交回复
热议问题