给定一个元组列表,我想要获取出现最频繁的元组,但如果有“并列获胜者”,则应随机选择其中一个。
tups = [ (1,2), (3,4), (5,6), (1,2), (3,4) ]
以上列表应在随机情况下返回
(1,2)
或 (3,4)
。使用collections.Counter
:
>>> collections.Counter([ (1,2), (3,4), (5,6), (1,2), (3,4) ]).most_common()[0]
((1, 2), 2)
这是 O(n log(n))
复杂度。
from collections import Counter
import random
tups = [ (1,2), (3,4), (5,6), (1,2), (3,4) ]
lst = Counter(tups).most_common()
highest_count = max([i[1] for i in lst])
values = [i[0] for i in lst if i[1] == highest_count]
random.shuffle(values)
print values[0]
O(nlogn)
。>>> tups = [ (1,2), (3,4), (5,6), (1,2), (3,4) ]
>>>
>>> sorted(tups)
[(1, 2), (1, 2), (3, 4), (3, 4), (5, 6)]
O(n)
的时间内完成任务:>>> from random import shuffle
>>> from collections import Counter
>>>
>>> tups = [(1,2), (3,4), (5,6), (1,2), (3,4)]
>>> c = Counter(tups) # count frequencies
>>> m = max(v for _, v in c.iteritems()) # get max frq
>>> r = [k for k, v in c.iteritems() if v == m] # all items with highest frq
>>> shuffle(r) # if you really need random - shuffle
>>> print r[0]
(3, 4)
O(n)
,考虑所有元素都是唯一的情况。 - simonzacklistAlphaLtrs = ['b','a','a','b','a','c','a','a','b','c','c','b','a','a','a']
dictFoundLtrs = {i:listAlphaLtrs.count(i) for i in listAlphaLtrs}
maxcnt = 0
theltr = 0
for ltr in dictFoundLtrs:
ltrfound = ltr
foundcnt = dictFoundLtrs[ltr]
if foundcnt > maxcnt:
maxcnt = foundcnt
theltr = ltrfound
print('most: ' + theltr)
来源:
使用 collections.Counter
进行计数,然后随机选择最常见的元素:
import collections
import random
lis = [ (1,2), (3,4), (5,6), (1,2), (3,4) ] # Test data
cmn = collections.Counter(lis).most_common() # Numbering based on occurrence
most = [e for e in cmn if (e[1] == cmn[0][1])] # List of those most common
print(random.choice(most)[0]) # Print one of the most common at random
O(1)
。在你的答案中,most_common()
方法将对所有元素进行排序,因此它将是最重要的部分 -O(n log(n))
。 - Roman Pekar