Skip to content
Advertisement

Sum a list of string based on rule

I have a huge python list as the following example:

ls = ['name: John', 'John has ', '4 yellow ', 'cars.', 'name: Angelina', 'Angelina has ', '5 yellow', 'cars.']

I would like to join this information in this formatting:

ls = ['name: John', 'John has 4 yellow cars.', 'name: Angelina', 'Angelina has 5 yellow cars.']

I have tried this code

  with open ('names.txt', 'r') as text:  
    lines = text.readlines()
    for index,line in enumerate(lines):
        if not linha.startswith('name:'):
            ls2.append(lines[index]+lines[index+1])

But it was not good, since I have something like:

ls = ['name: John', 'John has 4 yellow', '4 yellow cars.', 'cars.name: Angelina']

Do you have any idea how can I perform this task?

Advertisement

Answer

You can use itertools.groupby:

import itertools

ls = ['name: John', 'John has ', '4 yellow ', 'cars.', 'name: Angelina', 'Angelina has ', '5 yellow', 'cars.']

g = itertools.groupby(ls, lambda x: x.startswith('name: '))
output = [''.join(v) for _, v in g]
print(output) # ['name: John', 'John has 4 yellow cars.', 'name: Angelina', 'Angelina has 5 yellowcars.']

It groups the items by whether each item starts with 'name: ';

  1. Items that start with 'name: ' form a group (i.e., ['name: John']).
  2. Next a few items that don’t do so form a group (i.e., ['John has ', '4 yellow ', 'cars.']).
  3. Next items that do so form another group (['name: Angelina']).
  4. … and so on alternatingly.

Then join concatenates the strings in each group.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement