Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

Split only part of list in python

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...