繁体   English   中英

在列表列表中查找最常见的元素

[英]Finding the most common element in a list of lists

我得到了一个这样的列表列表:

pairs = [[(0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (3, 2), (3, 1), (2, 1), (3, 1), (3, 2), (3, 3), (3, 2), (2, 2)], 
         [(2, 2), (2, 1)], 
         [(1, 1), (1, 2), (2, 2), (2, 1)]]

并且所需的 output 是: {(2,2)}

我需要找到最常见的元素。 如果有重复多次的元素,它必须返回一个以上的值。

我尝试用三个列表的交集来解决它,但它打印出{(2,1), (2,2)} ,而不是{(2,2)} ,因为元素(2,2)重复了两次在第一个列表中的次数。

我看到了一些带有import collections的示例,但我不理解它们,所以我不知道如何更改代码以适合我的问题。

我还尝试了以下方法:

seen = set()
repeated = set()
for l in pairs:
    for i in set(l):
        if i in seen:
            repeated.add(i)
        if i in repeated:
            repeated.add(i)
        else:
            seen.add(i)

但仍然没有返回正确的答案。

不使用collections中的Counter方法的替代解决方案:

def get_freq_tuple(data):
    counts = {}
    max_count = 0
    for pairs in data:
        for pair in pairs:
            current_count = counts.get(pair, 0) + 1
            counts[pair] = current_count
            max_count = max(max_count, current_count)

    return [pair for pair in counts if counts[pair] == max_count]

if __name__ == "__main__":
    pairs = [[(0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (3, 2), (3, 1), (2, 1),
              (3, 1), (3, 2), (3, 3), (3, 2), (2, 2)],
             [(2, 2), (2, 1)],
             [(1, 1), (1, 2), (2, 2), (2, 1)]]
    print(get_freq_tuple(pairs))

Output:

[(2, 2)]

解释:

  • 计算每个元组的出现次数并将它们存储在字典中。 字典的键是元组,值是出现。
  • 按元组的最大出现次数过滤字典中的元组。

免责声明:

  • 使用collections中的Counter方法效率更高。

参考:

collections.Counter()将起作用......您只需要弄清楚如何传递嵌套列表中的所有对,您可以通过列表理解来完成。 例如:

from collections import Counter

pairs = [[(0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (3, 2), (3, 1), (2, 1), (3, 1), (3, 2), (3, 3), (3, 2), (2, 2)], 
         [(2, 2), (2, 1)], 
         [(1, 1), (1, 2), (2, 2), (2, 1)]]

counts = Counter(pair for l in pairs for pair in l)
counts.most_common(1)
# [((2, 2), 4)]

如果您有平局,则需要查看排名靠前的选项并挑选出具有相同数量的选项。 您可以通过查看counts.most_common()来获取排序列表。

itertools.groupby是处理此问题的常用方法。 例如,如果您有平局,您可以获得所有排名靠前的条目,例如:

from collections import Counter
from itertools import groupby

pairs = [[(0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (3, 2), (3, 1), (2, 1), (3, 1), (3, 2), (3, 3), (3, 2), (2, 2)], 
         [(2, 2), (2, 1)], 
         [(1, 1), (1, 2), (2, 2), (2, 1), (3, 2)]]

counts = Counter(pair for l in pairs for pair in l)

count, groups = next(groupby(counts.most_common(), key=lambda t: t[1]))
[g[0] for g in groups]
# [(2, 2), (3, 2)]

您可以使用collections.Counter

import collections

pairs = [[(0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (3, 2), (3, 1), (2, 1), (3, 1), (3, 2), (3, 3), (3, 2), (2, 2)],
         [(2, 2), (2, 1)],
         [(1, 1), (1, 2), (2, 2), (2, 1)]]

most_common = collections.Counter(tup for sublst in pairs for tup in sublst).most_common(1)
print(most_common) # [((2, 2), 4)]
print(*(tup[0] for tup in most_common)) # only the tuples: (2, 2)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM