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

Question:

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.

Asked By: ASH

||

Answers:

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
Answered By: j1-lee

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
Answered By: brunson
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.