Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain a strict alternating pattern of item "types" in a list?

Tags:

python

list

Given a list of strings, where each string is in the format "A - something" or "B - somethingelse", and list items mostly alternate between pieces of "A" data and "B" data, how can irregularities be removed?

  1. Irregularities being any sequence that breaks the A B pattern.
  2. If there are multiple A's, the next B should also be removed.
  3. If there are multiple B's, the preceding A should also be removed.
  4. After removal of these invalid sequnces, list order should be kept.

Example: A B A B A A B A B A B A B A B B A B A B A A B B A B A B

In this case, AAB (see rule 2), ABB (see rule 3) and AABB should be removed.

like image 209
kkoala Avatar asked Mar 22 '23 19:03

kkoala


2 Answers

I'll give it a try with regexp returning indexes of sequences to be removed

>>> import re
>>> data = 'ABABAABABABABABBABABAABBABAB'
>>> [(m.start(0), m.end(0)) for m in re.finditer('(AA+B+)|(ABB+)', data)]
[(4, 7), (13, 16), (20, 24)]

or result of stripping

>>> re.sub('(AA+B+)|(ABB+)', '', data)
ABABABABABABABABAB
like image 143
alko Avatar answered Apr 29 '23 15:04

alko


The drunk-on-itertools solution:

>>> s = 'ABABAABABABABABBABABAABBABAB'
>>> from itertools import groupby, takewhile, islice, repeat, chain
>>> groups = (list(g) for k,g in groupby(s))
>>> pairs = takewhile(bool, (list(islice(groups, 2)) for _ in repeat(None)))
>>> kept_pairs = (p for p in pairs if len(p[0]) == len(p[1]) == 1)
>>> final = list(chain(*chain(*kept_pairs)))
>>> final
['A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B', 'A', 'B']

(Unfortunately I'm now in no shape to think about corner cases and trailing As etc..)

like image 24
DSM Avatar answered Apr 29 '23 14:04

DSM