Py学习  »  Python

考虑序列的Python中重复值的计数

Peacepieceonepiece • 4 年前 • 1651 次点击  

我有 string 价值为:

s = 'asdabbdasfababbabb'

我已经分开了 str 通过使用以下代码,得到如下结果:

n = 3
split_strings = [s[index : index + n] for index in range(0, len(s), n)]

['asd', 'abb', 'das', 'fab', 'abb', 'abb']

我需要实现的是:

我想计算重复值,考虑到序列,例如:

({'asd': 1, 'abb': 1, 'das': 1, 'fab': 1, 'abb' : 2})

然而,如果我使用 Counter() 它计算重复的值,但似乎没有考虑列表的顺序:

Counter({'asd': 1, 'abb': 3, 'das': 1, 'fab': 1})

我怎样才能实现我所需要的?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/133042
文章 [ 2 ]  |  最新文章 4 年前