我是python的新手,想要从数组中提取最大的数量,
使用计数器, x =计数器(matchedAr) print x
我有以下列表。 专柜({6:452,5:439,2:391,9:379,7:361,4:324,0:313,3:310,8:274,1:248})
如何从此列表中提取最大数量,即6?
答案 0 :(得分:1)
您可以使用Counter.most_common()
>>> s
Counter({6: 452, 5: 439, 2: 391, 9: 379, 7: 361, 4: 324, 0: 313, 3: 310, 8: 274, 1: 248})
>>> s.most_common()[0][0]
6
>>>
OR:
Counter(list).most_commmon(1)
>>> s = [1,1,1,1,2,2,3,4,5]
>>> Counter(s).most_common(1)
[(1, 4)]
>>>
以下是帮助:
most_common(self, n=None) method of collections.Counter instance
List the n most common elements and their counts from the most
common to the least. If n is None, then list all element counts.
>>> Counter('abcdeabcdabcaba').most_common(3)
[('a', 5), ('b', 4), ('c', 3)]
>>>