gpt4 book ai didi

python - 如何统计连续字符串中字符出现的频率?

转载 作者:行者123 更新时间:2023-12-02 02:10:47 30 4
gpt4 key购买 nike

input = 'XXYXYYYXYXXYYY'

输出= [2,1,1,3,1,1,2,3]

如何按照输入顺序计算字符串中 X 和 Y 的数量,然后将这些值放入列表中?

最佳答案

import itertools
numbers = []
input = 'XXYXYYYXYXXYYY'
split_string = [''.join(g) for k, g in itertools.groupby(input)]

for i in split_string:
numbers.append(len(i))
print(numbers)

输出:

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

关于python - 如何统计连续字符串中字符出现的频率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59927814/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com