3

Given a list of tuples, I'm looking to get the most frequently occurring tuple BUT if there are "joint winners" it should pick between them at random.

tups = [ (1,2), (3,4), (5,6), (1,2), (3,4) ]

The above list should return either (1,2) or (3,4) at random for the above list

4

6 回答 6

11

Use collections.Counter:

>>> collections.Counter([ (1,2), (3,4), (5,6), (1,2), (3,4) ]).most_common()[0]
((1, 2), 2)

This is O(n log(n)).

于 2013-09-16T12:25:14.663 回答
2

您可以先使用 Counter 查找重复次数最多的元组。然后找到所需的元组,最后随机化,得到第一个值。

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]
于 2013-09-16T12:40:06.097 回答
1

您可以首先对列表进行排序以获取按频率排序的元组。之后,线性扫描可以让您从列表中获得最频繁的元组。总时间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)]
于 2013-09-16T12:24:29.260 回答
1

这个应该及时完成你的任务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)
于 2013-09-16T12:45:56.380 回答
0

计数,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
于 2013-09-16T12:49:46.043 回答
0

这是另一个没有导入的示例:

listAlphaLtrs = ['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)

资料来源:

https://stackoverflow.com/a/23240989/1447509

于 2019-04-30T17:34:03.917 回答