Split only part of list in python

2019-09-21 15:40发布

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条回答
Deceive 欺骗
2楼-- · 2019-09-21 16:43

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!

查看更多
登录 后发表回答