查找嵌套列表中最常见的整数出现

时间:2018-08-12 08:45:05

标签: python list count

我想找到在嵌套列表中最常出现的整数,并返回该整数及其出现次数(以及多个整数及其出现相同次数的出现次数)。数据当前为以下格式:

list_of_lists = 
    [[11, 53],
     [2, 137],
     [2, 7, 31],
     [2, 2, 7, 31],
     [3, 3, 3, 29],
     [2, 2, 2, 3, 137],
     [2, 2, 7, 31],
     [11, 53]]

因此,所需的输出为[[3, 3], [2, 3]],在第五个嵌套列表中3出现了3次,在第六个嵌套列表中3出现了3次。

列表和列表中的列表都不是固定长度的。因此,非常感谢能够解决此问题的程序!

我无法直接在点上找到类似的问题。

谢谢!

4 个答案:

答案 0 :(得分:0)

您可以使用container.Register( Classes.FromThisAssembly() .BasedOn<ApiController>() .LifestyleTransient() ); 对每个列表中元素的出现进行计数,然后根据出现的顺序以相反的顺序对结果列表进行排序,然后对结果进行分组(使用collections.Counter)以获取所有结果具有相同的最大值

itertools.groupby

答案 1 :(得分:0)

我使用了一个稍微复杂些的列表进行测试:一些值重复两次,大约3次,出现在相同和不同的子列表中。

我们在每个子列表中使用Counter,并保留我们为每个值看到的最高计数的字典。最后,我们构建输出列表,仅保留每行中重复次数最多的值。

list_of_lists =[[11, 11, 53], # 11 is repeated 2 times, 
 [2, 137],                    # it shouldn't appear in the result
 [2, 7, 31],
 [2, 2, 7, 31],
 [3, 3, 3, 4, 4, 4, 5, 5, 5, 29],     # 3 times 3, 4 and 5
 [2, 2, 2, 3, 137],                   # and 3 times 2
 [2, 2, 7, 31],
 [11, 53]]

from collections import Counter, defaultdict

def maxcount(list_of_lists):
    out = defaultdict(int)
    max_repetitions = 0
    for sublist in list_of_lists:
        for value, count in Counter(sublist).items():
            if count > 1 and count > out[value]:
                out[value] = count
                if count > max_repetitions:
                    max_repetitions = count


    return([[val, count] for val, count in out.items() if count == max_repetitions])

print(maxcount(list_of_lists))
# [[2, 3], [3, 3], [4, 3], [5, 3]]

我喜欢itertools,所以很好奇将@Sunitha的解决方案与此解决方案进行比较。

此解决方案:

*%timeit maxcount(list_of_lists)
# 65 µs ± 269 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

@Sunitha的解决方案,它更多地使用了itertools:

from itertools import chain, groupby
from collections import Counter

def maxcount_with_itertools(ll):
    f = lambda t: t[1]
    return list(next(groupby(sorted(chain(*(Counter(l).items() for l in ll)), key=f, reverse=True), f))[1])

%timeit maxcount_with_itertools(list_of_lists)
# 70.9 µs ± 1.39 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

有点慢。

答案 2 :(得分:0)

如果您有兴趣使用纯Python进行操作,则可以采用以下方法:

list_of_lists = [[11, 53],[2, 137],[2, 7, 31],[2, 2, 7, 31],[3, 3, 3, 29],[2, 2, 2, 3, 137],[2, 2, 7, 31],[11, 53]]

maxOccurences = [max([[elem,sublist.count(elem),index] for elem in sublist], key=lambda i:sublist.count(i)) for index, sublist in enumerate(list_of_lists)]
maximum = max(maxOccurences, key=lambda i: i[1])
elements = [elem[:2] for elem in maxOccurences if elem[1]==maximum[1]]
print(elements)

输出:

[[3, 3], [2, 3]]

另一个建议如下:

list_of_lists = [[11, 53],[2, 137],[2, 7, 31],[2, 2, 7, 31],[3, 3, 3, 29],[2, 2, 2, 3, 137],[2, 2, 7, 31],[11, 53]]

maximum = max([max([[elem,sublist.count(elem)] for elem in sublist], key=lambda i:sublist.count(i)) for sublist in list_of_lists], key=lambda i: i[1])
elements = [[elem,sublist.count(elem)] for sublist in list_of_lists for elem in set(sublist) if sublist.count(elem)==maximum[1]]
print(elements)

输出:

[[3, 3], [2, 3]]

答案 3 :(得分:0)

您可以使用collections.Counter,分为3个步骤:

  1. 通过Counter将列表转换为map个对象。
  2. 通过max计算最常用值的计数。
  3. 使用列表推导过滤来自子列表的Counter个对象。

这是一个演示。

from collections import Counter

counters = list(map(Counter, list_of_lists))
most_common_count = max(i.most_common(1)[0][1] for i in counters)

res = [(k, v) for i in counters for k, v in i.items() if v == most_common_count]

print(res)

[(3, 3), (2, 3)]
相关问题