python - 如何有效地计算字符串中字符频率的前缀和?

标签 python python-3.x string

说,我有一个字符串

s = 'AAABBBCAB'

如何有效计算字符串中每个字符的前缀频率总和,即:

psum = [{'A': 1}, {'A': 2}, {'A': 3}, {'A': 3, 'B': 1}, {'A': 3, 'B': 2}, {'A': 3, 'B': 3}, {'A': 3, 'B': 3, 'C': 1}, {'A': 4, 'B': 3, 'C': 1}, {'A': 4, 'B': 4, 'C': 1}]

最佳答案

您可以使用 itertools.accumulate 在一行中完成。和 collections.Counter :

from collections import Counter
from itertools import accumulate

s = 'AAABBBCAB'
psum = list(accumulate(map(Counter, s)))

这会为您提供 Counter 对象的列表。现在,要在 O(1) 时间内获取 s 的任何子字符串的频率,您可以简单地减去计数器,例如:

>>> psum[6] - psum[1]  # get frequencies for s[2:7]
Counter({'B': 3, 'A': 1, 'C': 1})

关于python - 如何有效地计算字符串中字符频率的前缀和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55904191/

相关文章:

python - 有 Numpy asanyarray 与 asarray 的例子吗?

python - 使用多进程杀死程序

python - 如何在没有 QProcess 的情况下将终端嵌入到 PyQt5 应用程序中?

python - 从字符串 Python 中删除序列中的项目

python - 如何通过PyQt5提交数据并搜索

python - 几个小时后 tweepy 停止

java - Java 中使用适当截断的 byte[] 到 String 转换

c++ - 命名空间 std 中的字符串没有命名类型

python - 继承时避免 __init__ 和 super 样板

python - 将日期时间值转换为整数