Split only part of list in python

[亡魂溺海] 提交于 2019-12-13 22:33:21

问题


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!



来源:https://stackoverflow.com/questions/31433377/split-only-part-of-list-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!