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

  • A+
Category:Languages

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!

 


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

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: