Python 中查找字符串中出现次数最多的字符
Python 中要查找字符串中出现次数最多的字符:
-
使用
collections.Counter()
类创建一个计数器对象。 -
使用
most_common()
方法获取字符串中出现频率最高的字符。
from collections import Counter
string = 'aaabbc'
counter = Counter(string)
# ✅ Find most frequent character in string
most_frequent = counter.most_common(1)[0]
print(most_frequent) # 👉️ ('a', 3)
print(most_frequent[0]) # 👉️ 'a'
# ✅ Find N most frequent characters in string
most_frequent_2 = counter.most_common(2)
print(most_frequent_2) # 👉️ [('a', 3), ('b', 2)]
我们使用 collections.Counter
类来计算每个字符的出现次数。
collections 模块中的 Counter 类是 dict 类的子类。
该类基本上是键数对的映射。
from collections import Counter
string = 'aaabbc'
counter = Counter(string)
print(counter) # 👉️ Counter({'a': 3, 'b': 2, 'c': 1})
Counter
对象实现了 most_common()
方法,该方法返回 N 个最常见元素的列表及其从最常见到最少的计数。
from collections import Counter
string = 'aaabbc'
counter = Counter(string)
print(counter) # 👉️ Counter({'a': 3, 'b': 2, 'c': 1})
most_frequent = counter.most_common(1)[0]
print(most_frequent) # 👉️ ('a', 3)
print(most_frequent[0]) # 👉️ 'a'
Python 索引是从零开始的,因此列表中的第一项的索引为 0,最后一项的索引为 -1 或 len(my_list) - 1
。
如果没有向 most_common 方法提供参数,它会列出所有字符数。
该方法返回一个元组列表,其中第一个元素是字符,第二个元素是出现次数。
我们还可以使用此方法获取字符串中最常见的 N 个字符。
from collections import Counter
string = 'aaabbc'
counter = Counter(string)
print(counter) # 👉️ Counter({'a': 3, 'b': 2, 'c': 1})
most_frequent_2 = counter.most_common(2)
print(most_frequent_2) # 👉️ [('a', 3), ('b', 2)]
我们将 2 作为参数传递给 most_common()
方法,因此它返回字符串中最常见的 2 个字符。
或者,我们可以使用 max()
函数。
使用 max() 查找字符串中出现频率最高的字符
要查找字符串中出现次数最多的字符:
-
使用
max()
函数。 - 将 key 参数传递给函数。
-
使用
str.count()
方法计算每个字符的出现次数。 -
max()
函数将返回最常见的字符。
string = 'aaabbc'
most_common = max(set(string), key=string.count)
print(most_common) # 👉️ 'a'
我们使用 max()
函数来获取字符串中最常见的字符。
max()
函数返回可迭代对象中的最大项或两个或多个参数中最大的一个。
my_list = [15, 45, 30]
result = max(my_list)
print(result) # 👉️ 45
max
函数也有一个可选的键参数。
key
参数指定一个单参数排序函数。
我们使用 set()
类将字符串转换为集合对象以删除任何重复项。
你可以想象:
-
集合中的每个元素都会调用
str.count()
方法。 - 该方法返回字符在字符串中出现的次数。
-
max()
函数返回最常见的字符。
string = 'aaabbc'
most_common = max(set(string), key=string.count)
print(most_common) # 👉️ 'a'
str.count
方法返回字符串中子字符串的出现次数。
print('jiyik'.count('i')) # 👉️ 2
print('jiyik'.count('l')) # 👉️ 0
或者,我们可以使用 statistics.mode()
方法。
使用 statistics.mode() 查找字符串中出现频率最高的字符
使用 statistics.mode()
方法查找字符串中出现频率最高的字符,例如 most_common = mode(string)
。 mode()
方法返回提供的可迭代对象中最常见的单个值。
from statistics import mode
string = 'aaabbc'
most_common = mode(string)
print(most_common) # 👉️ 'a'
statistics.mode
方法接受一个可迭代对象并返回可迭代对象中最常见的值。
如果有多个值具有相同的频率,则该方法返回第一个遇到的值。
如果提供的可迭代对象为空,则该方法会引发 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 日期时间。