簡體   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