count consecutive elements

Wolfram Hinderer wolfram.hinderer at googlemail.com
Thu Jan 14 17:00:40 EST 2021


Am 13.01.2021 um 22:20 schrieb Bischoop:
> I want to  to display a number or an alphabet which appears mostly
> consecutive in a given string or numbers or both
> Examples
> s= ' aabskaaabadcccc'
> output: c
> # c appears 4 consecutive times
>   8bbakebaoa
> output: b
> #b appears 2 consecutive times
>
>
You can let itertools.groupy find the groups.

max((len(tuple(group)), key) for key, group in itertools.groupby(s))
# (4, 'c')


More information about the Python-list mailing list