Home > Mobile >  Value switcher for groups in Python/Numpy
Value switcher for groups in Python/Numpy

Time:11-10

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.

CodePudding user response:

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])

CodePudding user response:

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
  • Related