在 Python 中查找列表中最常见的元素
Python 中要查找列表中最常见的元素:
-
使用
collections.Counter()
类创建一个计数器对象。 -
使用
most_common()
方法获取列表中最常见的元素。
from collections import Counter
a_list = ['one', 'one', 'one', 'two', 'two', 'three']
counter = Counter(a_list)
most_common = counter.most_common(1)[0][0]
print(most_common) # 👉️ one
most_common_2 = counter.most_common(2)
print(most_common_2) # 👉️ [('one', 3), ('two', 2)]
我们使用 collections.Counter
类来计算每个列表项的出现次数。
collections
模块中的 Counter 类是 dict 类的子类。
该类基本上是键数对的映射。
from collections import Counter
a_list = ['one', 'one', 'one', 'two', 'two', 'three']
counter = Counter(a_list)
print(counter) # 👉️ Counter({'one': 3, 'two': 2, 'three': 1})
Counter 对象实现了 most_common()
方法,该方法返回 N 个最常见元素的列表及其从最常见到最少的计数。
from collections import Counter
a_list = ['one', 'one', 'one', 'two', 'two', 'three']
counter = Counter(a_list)
most_common = counter.most_common(1)[0][0]
print(most_common) # 👉️ one
# 👇️ [('one', 3)]
print(counter.most_common(1))
Python 索引是从零开始的,因此列表中的第一项的索引为 0,最后一项的索引为 -1 或 len(my_list) - 1
。
如果没有向 most_common
方法提供参数,它将列出所有元素计数。
该方法返回一个元组列表,其中第一个元素是列表项,第二个元素是出现的次数。
我们还可以使用此方法获取列表中最常见的 N 个元素。
from collections import Counter
a_list = ['one', 'one', 'one', 'two', 'two', 'three']
counter = Counter(a_list)
n = 2
most_common_n = counter.most_common(n)
print(most_common_n) # 👉️ [('one', 3), ('two', 2)]
我们将 2 作为参数传递给 most_common()
方法,因此它返回列表中最常见的 2 个元素。
或者,我们可以使用 max()
函数。
使用 max() 查找列表中最常见的元素
要查找列表中最常见的元素:
-
使用
max()
函数。 - 将 key 参数传递给函数。
-
使用
list.count()
方法计算每个元素的出现次数。 -
max()
函数将返回最常见的元素。
a_list = ['one', 'one', 'one', 'two', 'two', 'three']
most_common = max(set(a_list), key=a_list.count)
print(most_common) # 👉️ one
我们使用 max()
函数来获取列表中最常见的元素。
max()
函数返回可迭代对象中的最大项或两个或多个参数中最大的一个。
my_list = [15, 45, 30]
result = max(my_list)
print(result) # 👉️ 45
max
函数也有一个可选的键参数。
key 参数指定一个单参数排序函数。
我们使用 set()
类将列表转换为集合对象以删除任何重复项。
你可以想象:
-
集合中的每个元素都会调用
list.count()
方法。 - 该方法返回元素在列表中出现的次数。
-
max()
函数返回最常见的列表元素。
a_list = ['one', 'one', 'one', 'two', 'two', 'three']
most_common = max(set(a_list), key=a_list.count)
print(most_common) # 👉️ one
list.count()
方法接受一个值并返回所提供的值在列表中出现的次数。
a_list = ['one', 'one', 'one', 'two', 'two', 'three']
print(a_list.count('one')) # 👉️ 3
print(a_list.count('two')) # 👉️ 2
print(a_list.count('abc')) # 👉️ 0
或者,我们可以使用 statistics.mode()
方法。
使用 statistics.mode()
查找列表中最常见的元素
使用 statistics.mode()
方法查找列表中最常见的元素,例如 most_common = mode(a_list)
。 mode()
方法返回提供的可迭代对象中最常见的单个值。
from statistics import mode
a_list = ['one', 'one', 'one', 'two', 'two', 'three']
most_common = mode(a_list)
print(most_common) # 👉️ one
statistics.mode()
方法接受一个可迭代对象并返回可迭代对象中最常见的值。
from statistics import mode
a_list = ['one', 'one', 'one', 'two', 'two', 'three']
print(mode([1, 1, 1, 2, 2, 3])) # 👉️ 1
# 👇️ one
print(mode(['one', 'one', 'one', 'two', 'two', 'three']))
如果有多个值具有相同的频率,则该方法返回第一个遇到的值。
如果提供的可迭代对象为空,则该方法会引发 StatisticsError 异常。
在 Python v3.8 之前,如果可迭代对象中没有最常见的元素,则 statistics.mode()
方法会引发 StatisticsError
异常,例如 如果前两个出现的次数相同。
相关文章
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中将 Timedelta 转换为 Int
发布时间:2024/04/23 浏览次数:231 分类:Python
-
可以使用 Pandas 中的 dt 属性将 timedelta 转换为整数。
Python 中的 Pandas 插入方法
发布时间:2024/04/23 浏览次数:112 分类:Python
-
本教程介绍了如何在 Pandas DataFrame 中使用 insert 方法在 DataFrame 中插入一列。
使用 Python 将 Pandas DataFrame 保存为 HTML
发布时间:2024/04/21 浏览次数:106 分类:Python
-
本教程演示如何将 Pandas DataFrame 转换为 Python 中的 HTML 表格。
如何将 Python 字典转换为 Pandas DataFrame
发布时间:2024/04/20 浏览次数:73 分类:Python
-
本教程演示如何将 python 字典转换为 Pandas DataFrame,例如使用 Pandas DataFrame 构造函数或 from_dict 方法。
如何在 Pandas 中将 DataFrame 列转换为日期时间
发布时间:2024/04/20 浏览次数:101 分类:Python
-
本文介绍如何将 Pandas DataFrame 列转换为 Python 日期时间。