Python 字典中的顶级值

Python 字典中的顶级值,python,dictionary,Python,Dictionary,如何从字典中检索前三名 >>> d {'a': 2, 'and': 23, 'this': 14, 'only.': 21, 'is': 2, 'work': 2, 'will': 2, 'as': 2, 'test': 4} 预期结果: and: 23 only: 21 this: 14 使用: 计数器对象还提供了其他各种优势,例如,首先收集计数几乎是微不足道的。您已经得到的答复是正确的,但是我会创建自己的键函数,以便在调用排序()时使用 >>> d =

如何从字典中检索前三名

>>> d
{'a': 2, 'and': 23, 'this': 14, 'only.': 21, 'is': 2, 'work': 2, 'will': 2, 'as': 2, 'test': 4}
预期结果:

and: 23
only: 21
this: 14
使用:


计数器对象还提供了其他各种优势,例如,首先收集计数几乎是微不足道的。

您已经得到的答复是正确的,但是我会创建自己的键函数,以便在调用排序()时使用

>>> d = {'a': 2, 'and': 23, 'this': 14, 'only.': 21, 'is': 2, 'work': 2, 'will': 2, 'as': 2, 'test': 4}
>>> t = sorted(d.iteritems(), key=lambda x:-x[1])[:3]

>>> for x in t:
...     print "{0}: {1}".format(*x)
... 
and: 23
only.: 21
this: 14

鉴于上述解决方案:

def most_popular(L):
  # using lambda
  start = datetime.datetime.now()
  res=dict(sorted([(k,v) for k, v in L.items()], key=lambda x: x[1])[-2:])
  delta=datetime.datetime.now()-start
  print "Microtime (lambda:%d):" % len(L), str( delta.microseconds )

  # using collections
  start=datetime.datetime.now()
  res=dict(collections.Counter(L).most_common()[:2])
  delta=datetime.datetime.now()-start
  print "Microtime (collections:%d):" % len(L), str( delta.microseconds )

# list of 10
most_popular({el:0 for el in list(range(10))})

# list of 100
most_popular({el:0 for el in list(range(100))})

# list of 1000
most_popular({el:0 for el in list(range(1000))})

# list of 10000
most_popular({el:0 for el in list(range(10000))})

# list of 100000
most_popular({el:0 for el in list(range(100000))})

# list of 1000000
most_popular({el:0 for el in list(range(1000000))})
处理大小为10^1到10^6的数据集dict对象,如

print {el:0 for el in list(range(10))}
{0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
我们有以下基准

Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.8.2] on linux

Microtime (lambda:10): 24
Microtime (collections:10): 106
Microtime (lambda:100): 49
Microtime (collections:100): 50
Microtime (lambda:1000): 397
Microtime (collections:1000): 178
Microtime (lambda:10000): 4347
Microtime (collections:10000): 2782
Microtime (lambda:100000): 55738
Microtime (collections:100000): 26546
Microtime (lambda:1000000): 798612
Microtime (collections:1000000): 361970
=> None
所以我们可以说,对于小列表,使用
lambda
,但是对于大列表,
集合
具有更好的性能


查看运行的基准测试。

如果您还想计算事物,那么同意计数器是更好的方法。但是,如果您只想在已经创建的dict中使用前3个值,那么这似乎是一种过度使用。:)这取决于字典的大小。对字典进行排序是O(n log n),创建一个计数器并提取
k
最大值仅为O(n log k)。对于大的
n
和小的
k
,这使得计数器选项更加有效。实际上,对于3个最大值,我会使用
heapq.nlargest()函数;它比对整个序列进行排序更有效。这就是
Counter()
内部使用的。
print {el:0 for el in list(range(10))}
{0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.8.2] on linux

Microtime (lambda:10): 24
Microtime (collections:10): 106
Microtime (lambda:100): 49
Microtime (collections:100): 50
Microtime (lambda:1000): 397
Microtime (collections:1000): 178
Microtime (lambda:10000): 4347
Microtime (collections:10000): 2782
Microtime (lambda:100000): 55738
Microtime (collections:100000): 26546
Microtime (lambda:1000000): 798612
Microtime (collections:1000000): 361970
=> None