Home > Back-end >  python:split a list of strings into multiple lists before a certain value
python:split a list of strings into multiple lists before a certain value

Time:02-19

I have a list as follows,

my_list = ['France, the weather is nice', 'the house is beautiful', 'we have a fresh start here','France, the dinner is amazing', 'the lady is lovely', 'France, the house is beautiful','the location is fascinating']

I want to split the list into multiple list every time the word 'France,' is in the string, so the output should be,

desired_list = [['France, the weather is nice', 'the house is beautiful', 'we have a fresh start here'],['France, the dinner is amazing', 'the lady is lovely'],['France, the house is beautiful','the location is fascinating']]

I have tried the following, but would like to keep the sentence that has 'France,' too.

new_list =[list(y) for x, y in itertools.groupby(my_list, lambda z: z.startswith('France,)) if not x]

another answer

in addition to the answer given by Titouan L, I also found the answer using more_itertools

import more_itertools as miter
print(list(miter.split_before(my_list, lambda x: x.startswith("France,"))))

CodePudding user response:

I've come up with a solution with nested loops, as I'm not very experienced with list comprehension and lambda expression.

my_list = ['France, the weather is nice', 'the house is beautiful', 'we have a fresh start here', 'France, the dinner is amazing',
           'the lady is lovely', 'France, the house is beautiful', 'the location is fascinating']

new_list = []
sub_list = []
for i in my_list:
    if i.startswith('France,'):
        if sub_list != []:
            new_list.append(sub_list)
            sub_list=[]

    sub_list.append(i)
new_list.append(sub_list)

CodePudding user response:

Just for fun, using recursion:

my_list = ['France, the weather is nice', 'the house is beautiful', 'we have a fresh start here', 'France, the dinner is amazing', 'the lady is lovely', 'France, the house is beautiful','the location is fascinating']


def split_list(index=len(my_list) - 1):
    if index < 0:
        return []
    if not my_list[index].startswith('France,'):
        data = split_list(index - 1)
        data[-1].append(my_list[index])
        return data
    else:
        return split_list(index - 1)   [[my_list[index]]]


print(split_list())

Output:

[['France, the weather is nice', 'the house is beautiful', 'we have a fresh start here'], ['France, the dinner is amazing', 'the lady is lovely'], ['France, the house is beautiful', 'the location is fascinating', 'the location is fascinating']]

Definitely not the most efficient :)

  • Related