Home > Blockchain >  How can we count text 'changes' in a list?
How can we count text 'changes' in a list?

Time:09-09

How can we count text 'changes' in a list?

The list below has 0 'changes'. Everything is the same.

['Comcast', 'Comcast', 'Comcast', 'Comcast', 'Comcast', 'Comcast']

The list below has 3 changes. First change is Comcast>Sprint, second change is Sprint>AT&T and third change is AT&T>Comcast.

['Comcast', 'Comcast', 'Sprint', 'AT&T', 'Comcast', 'Comcast']

I Googled this before posting here. Finding unique items seems pretty easy. Finding changes, or switches, seems not so easy.

CodePudding user response:

One option is to use itertools.groupby. This counts the number of "chunks" and subtract one (to get the "boundaries").

from itertools import groupby

lst = ['Comcast', 'Comcast', 'Sprint', 'AT&T', 'Comcast', 'Comcast']

output = sum(1 for _ in groupby(lst)) - 1
print(output) # 3

CodePudding user response:

You want to compare elements pairwise, so you can create an iterator to pair up adjacent elements:

>>> l = [ 1, 1, 2, 3, 1, 1, 1, ]
>>> list(zip(l[:-1], l[1:]))
[(1, 1), (1, 2), (2, 3), (3, 1), (1, 1), (1, 1)]

Then iterate over them and test if they're pairwise equal:

>>> [x == y for (x, y) in zip(l[0:-1], l[1:])]
[True, False, False, False, True, True]

Then count them where they are not equal:

>>> sum(1 for (x, y) in zip(l[0:-1], l[1:]) if x != y)
3
  • Related