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: '
;
- Items that start with
'name: '
form a group (i.e.,['name: John']
). - Next a few items that don’t do so form a group (i.e.,
['John has ', '4 yellow ', 'cars.']
). - Next items that do so form another group (
['name: Angelina']
). - … and so on alternatingly.
Then join
concatenates the strings in each group.