Value switcher for groups in Python/Numpy

Question:

I have a list:

groups = ['A', 'A', 'A', 'B', 'B', 'C', 'C', 'D']

I need to map each value to have an output like this, independently from the numbers of groups and elements inside:

[0,0,0,1,1,0,0,1]

The values in output should switch every time when the group is changing.

Asked By: Krzyztopher

||

Answers:

Using

With a list comprehension and the walrus operator of python 3.8+:

groups = ['A', 'A', 'A', 'B', 'B', 'C', 'C', 'D']

flag = 0

out = [flag if a==b else (flag:=1-flag) for a, b in zip(groups, groups[:1]+groups)]

Or itertools:

from itertools import groupby, chain

out = list(chain.from_iterable([i%2]*len(list(g))
           for i, (_, g) in enumerate(groupby(groups))))

Output:

[0, 0, 0, 1, 1, 0, 0, 1]

Using :

import pandas as pd

out = pd.factorize(groups)[0]%2

Output:

array([0, 0, 0, 1, 1, 0, 0, 1])

Or:

s = pd.Series(groups)
out = (s.ne(s.shift(fill_value=s[0]))
       .cumsum().mod(2).tolist()
       )

Output:

[0, 0, 0, 1, 1, 0, 0, 1]

Using :

import numpy as np

out = np.cumsum(groups != np.r_[groups[:1], groups[:-1]])%2

Output:

array([0, 0, 0, 1, 1, 0, 0, 1])
Answered By: mozway

Here’s another O(n) solution:

def value_switcher(groups):
    if not groups:
        return groups
    new_groups = [0]
    for i in range(1, len(groups)):
        if groups[i-1] != groups[i]:
            if new_groups[i-1] == 0:
                new_groups.append(1)
            else:
                new_groups.append(0)
        else:
            new_groups.append(new_groups[i-1])
    return new_groups

I tested it as follows:

groups = ['A', 'A', 'A', 'B', 'B', 'C', 'C', 'D']
assert value_switcher(groups) == [0, 0, 0, 1, 1, 0, 0, 1]
groups = ['A']
assert value_switcher(groups) == [0]
groups = ['B', 'A', 'B', 'B', 'A']
assert value_switcher(groups) == [0, 1, 0, 0, 1]
groups = []
assert value_switcher(groups) == []
groups = None
assert value_switcher(groups) is None
Answered By: Nick Falco
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.