Sort a dictionary by length of the value [closed]

2020-07-30 03:15发布

I have a dictionary:

d = {'a':[1,3], 'b':[3,4,5,6], 'c':[1], 'd':[1,2,3] }

I want to make a smaller, new dictionary with the top two key:value pairs, sorted by the len of the lists in value. So in this case, I want:

newd = {'b':[3,4,5,6], 'd':[1,2,3] }

I tried this answer but got this error:

NameError: global name 'd' is not defined

2条回答
何必那么认真
2楼-- · 2020-07-30 03:59

One approach is to sort the items according to length of value, and then create a dictionary from the last two items.

sorted_items = sorted(d.items(), key = lambda item : len(item[1]))
newd = dict(sorted_items[-2:])
查看更多
贪生不怕死
3楼-- · 2020-07-30 03:59

Seems like a job for heapq:

big_items = heapq.nlargest(2, d.items(), key=lambda x: len(x[1]))
newd = dict(big_items)

The advantage of heapq over sorted is that this provides an O(N) time complexity whereas sorted will yield an O(NlogN) time complexity. For small dicts, this probably doesn't matter much. sorted may even be faster due to a more optimized implementation, but for big dicts, this might actually buy you a significant speedup.


(on python2.x, you can use d.iteritems() instead of d.items())

查看更多
登录 后发表回答