Home > front end >  Count duplicated value in Python considering sequence
Count duplicated value in Python considering sequence

Time:04-27

I have string value as:

s = 'asdabbdasfababbabb'

I've split the str by using below code, than get result as below :

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

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

What I need to achieve:

I want to count duplicated value consiering the sequence such as :

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

However, if I use Counter() it counts the duplicated value but, does not seems to consider the sequence of list:

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

How can I achieve what I need?

CodePudding user response:

You cannot store duplicate keys in a dict. If you are willing to have a list of tuples, you can use itertools.groupby:

from itertools import groupby

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

counts = [(k, len([*g])) for k, g in groupby(lst)]
print(counts) # [('asd', 1), ('abb', 1), ('das', 1), ('fab', 1), ('abb', 2)]

CodePudding user response:

The itertools.groupby function is a favorite, but perhaps future readers might appreciate an algorithm for actually finding these groupings:

def groups(*items):
    i = 0
    groups = []
    while i < len(items):
        item = items[i]
        j = i   1
        count = 1
        while j < len(items):
            if items[j] == item:
                count  = 1
                j  = 1
            else:
                break
        i = j
        groups.append((item, count))
    return groups
  • Related