Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to combine or leave strings in the lists depending on the condition in python?

Tags:

python

list

I have three lists:

li1 = ["a", "a", "a", "a", "b", "b", "a", "a", "b"]
li2 = ["a", "a", "a", "b", "a,", "b", "a", "a"]
li3 = ["b", "b", "a", "a", "b"]

I want to "slice and paste" elements by "b"

The result is supposed to look like this:

li1 = ["aaaa", "b", "b", "aa", "b"]
li2 = ["aaa", "b", "a", "b", "aa"]
li3 = ["b", "b", "aa", "b"]

But I don't know how to approach this... please help me!

like image 417
gukwon Avatar asked Dec 23 '22 02:12

gukwon


1 Answers

Use itertools.groupby.

If you want to join groups not belonging to a certain key

from itertools import groupby

def join_except_key(iterable, key='b'):
    groups = groupby(iterable)
    for k, group in groups:
        if k != key:
            yield ''.join(group) # more general: ''.join(map(str, group))
        else:
            yield from group

Demo:

>>> li1 = ["a", "a", "a", "a", "b", "b", "a", "a", "b", "c", "c", "b", "c", "c"]
>>> list(join_except_key(li1))
['aaaa', 'b', 'b', 'aa', 'b', 'cc', 'b', 'cc']

If you want to join groups belonging to a certain key

from itertools import groupby

def join_by_key(iterable, key='a'):
    groups = groupby(iterable)
    for k, group in groups:
        if k == key:
            yield ''.join(group) # more general: ''.join(map(str, group))
        else:
            yield from group

Demo:

>>> li1 = ["a", "a", "a", "a", "b", "b", "a", "a", "b", "c", "c", "b", "c", "c"]
>>> list(join_by_key(li1))
['aaaa', 'b', 'b', 'aa', 'b', 'c', 'c', 'b', 'c', 'c']

Details on what groupby produces (non generator approach for join_except_key)

>>> li1 = ["a", "a", "a", "a", "b", "b", "a", "a", "b", "c", "c", "b", "c", "c"]
>>> groups = [(k, list(group)) for k, group in groupby(li1)]
>>> groups
[('a', ['a', 'a', 'a', 'a']),
 ('b', ['b', 'b']),
 ('a', ['a', 'a']),
 ('b', ['b']),
 ('c', ['c', 'c']),
 ('b', ['b']),
 ('c', ['c', 'c'])]
>>>
>>> result = []
>>> for k, group in groups:
...:    if k != 'b':
...:        result.append(''.join(group))
...:    else:
...:        result.extend(group)
...:
>>> result
['aaaa', 'b', 'b', 'aa', 'b', 'cc', 'b', 'cc']

The list comprehension groups = [... in the second line was only needed for inspecting the elements of the grouping operation, it works fine with just groups = groupby(li1).

like image 122
timgeb Avatar answered Dec 25 '22 15:12

timgeb