Split only part of list in python

2019-09-21 16:27发布

问题:

I have a list

['Paris, 458 boulevard Saint-Germain', 'Marseille, 29 rue Camille Desmoulins', 'Marseille, 1 chemin des Aubagnens']

i want split after keyword "boulevard, rue, chemin" like in output

['Saint-Germain', 'Camille Desmoulins', 'des Aubagnens']

Thanks for your time

回答1:

It's not working because you are only extracting one word after you split:

adresse = [i.split(' ', 8)[3] for i in my_list`]

due to [3].

Try [3:] instead. Ah, but that still won't be enough, because you'll get a list of lists, when you want a list of strings. So you also need to use join.

adresse = [' '.join(i.split(' ', 8)[3:]) for i in my_list`]

Now your only difficulty is dealing with irregular street addresses, e.g. people who don't have a house number, or several words in the street name. I have no solution for that!