【问题标题】:How to find most common element in a list of list?如何在列表列表中找到最常见的元素?
【发布时间】:2012-11-12 19:28:28
【问题描述】:

我明白了

a = max(set(lst), key=lst.count)

将导出列表中最常见的元素

但是如何在不使用辅助函数的情况下导出列表列表中最常见的元素?

例如

lst = [['1','2','3','4'],['1','1','1','1'],['1','2','3','4']]

输出应该等于1

当我尝试a = max(set(lst), key=lst.count)

它写builtins.TypeError: unhashable type: 'list'

谁能帮帮我?

【问题讨论】:

  • 您对[['1', '2', '3', '4'], ['1', '1', '2', '2'], ['2', '2', '3', '4']]的预期输出是什么?
  • “辅助功能”是什么意思?您的定义似乎包括Counter,但不包括maxset。与使用标准库相比,您最好考虑避免 O(N^2) 行为(例如在您的 max(set(lst), key=list.count) 中)。

标签: python list python-3.x count python-2.7


【解决方案1】:

有很多方法,但我想让您知道,标准模块中有一些很好的工具可以处理这类事情,例如collections.Counter:

In [1]: lst = [['1','2','3','4'],['1','1','1','1'],['1','2','3','4']]
In [2]: from collections import Counter
In [3]: from operator import itemgetter
In [4]: max((Counter(l).most_common(1)[0] for l in lst), key=itemgetter(1))[0]
Out[4]: '1'

或者,您可以(有点)为每个子列表使用您当前的解决方案:

In [5]: max(((max(set(l), key=l.count), l) for l in lst),
   ...: key=lambda x: x[1].count(x[0]))[0]
Out[5]: '1'

【讨论】:

  • 真的非常感谢您提供的代码,但是您还有其他不需要 itemgetter 或 counter 的功能吗?所以我的意思是简单的python编程,不需要任何from语句或import语句
  • 优秀的 Python 代码利用了已经可用的优秀模块。您是否有任何理由要避免使用内置模块?这是作业吗?
  • 不,我会说这是家庭作业的一部分,可以在不使用太多 if 语句的情况下缩短函数,这是 Python 课程的介绍。
  • @EricJung 好吧,我添加了另一个示例,部分使用了您发布的代码,但第一个版本对我来说似乎更好。
【解决方案2】:

只需将您的list of list 展平,然后在其上使用collections.Counter。然后使用Counter.most_common() 方法获得list of tuple 的元素,其出现次数从高到低:-

>>> lst = [['1','2','3','4'],['1','1','1','1'],['1','2','3','4']]
>>> flattened_list = [elem for sublist in lst for elem in sublist]  
>>> flattened_list
['1', '2', '3', '4', '1', '1', '1', '1', '1', '2', '3', '4']
>>>
>>> from collections import Counter
>>>
>>> counter = Counter(flattened_list)
>>> counter.most_common()
[('1', 6), ('3', 2), ('2', 2), ('4', 2)]
>>>
>>> counter.most_common(1)
('1', 6)

或者,您可以使用您的方法从flatten 列表中获取最常见的元素。

>>> max(set(flattened_list), key=flattened_list.count)
'1'

你也可以像这样扁平化你的列表:-

>>> sum(lst, [])
['1', '2', '3', '4', '1', '1', '1', '1', '1', '2', '3', '4']

所以,作为一个单行,你可以这样做:-

>>> lst = [['1','2','3','4'],['1','1','1','1'],['1','2','3','4']]

>>> max(set(sum(lst, [])), key=sum(lst, []).count)
'1'

当然,最后一个会创建两个列表,内容相同。

【讨论】:

  • most_common 将项目数作为参数。
【解决方案3】:

您必须展平您的列表(使用chain(*lst)),然后使用Counter(chain(*lst).most_common()) 计算列表中每个元素的条目并对结果进行排序。

from itertools import chain
from collections import Counter

lst = [['1','2','3','4'],['1','1','1','1'],['1','2','3','4']]
sorted(Counter(chain(*lst)).most_common())[0][0]

【讨论】:

  • 也尝试提供评论或解释。谢谢。
【解决方案4】:

您可以使用Counter 查找最常见的元素,并使用chain 遍历列表列表的元素:

from collections import Counter
from itertools import chain

print Counter(val for val in chain.from_iterable(lst)).most_common(1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-05
    • 2010-12-03
    • 1970-01-01
    相关资源
    最近更新 更多