python - Extract departure and arrival from a list
问题描述
I'm trying to extract some parameters from a list whose the structure and the length are variable. Basically, these parameters are the departure and the arrival addresses for a route. This list is built from a sentence in natural language so it does not follow any particular template:
1st example : ['go', 'Buzenval', 'from', 'Chatelet']
2nd example : ['How', 'go', 'street', 'Saint', 'Augustin', 'from', 'Buzenval']
3rd example : ['go', 'from', '33', 'street', 'Republique', 'to', '12','street','Napoleon']
I already managed to create another list that is pretty much similar, for each case, except the departure and arrival are replaced by the actual words 'departure' and 'arrival'. With the examples above I obtain:
1st example : ['go', 'arrival', 'from', 'departure']
2nd example : ['How', 'go', 'arrival', 'from', 'departure']
3rd example : ['go', 'from', 'departure', 'to', 'arrival']
Now that I have these two kind of lists, I would like to identify departure and arrival :
1rst example : departure = ['Chatelet'], arrival = ['Buzenval']
2nd example : departure = ['Buzenval'], arrival = ['street','Saint','Augustin']
3rd example : departure = ['33','street','Republique'], arrival = ['12','street','Napoleon']
Basically, the parameters are everything that are different in the two lists, but I need to identify which one is the departure and which one is the arrival. I think Regex could help me on this one but I don't know how.
Thanks for your help!
解决方案
这应该适合你:
l1 = ['go', 'Buzenval', 'from', 'Chatelet']
l2 = ['How', 'go', 'street', 'Saint', 'Augustin', 'from', 'Buzenval']
l3 = ['go', 'from', '33', 'street', 'Republique', 'to', '12','street','Napoleon']
def get_locations (inlist):
marker = 0
end_dep = 0
start_dep = 0
for word in inlist:
if word =="go":
if inlist[marker+1] != "from":
end_dep = marker +1
else:
start_dep = marker +2
if word =="from" and start_dep == 0:
start_dep = marker + 1
if word == "to":
end_dep = marker + 1
marker +=1
if end_dep > start_dep:
start_loc = inlist[start_dep:end_dep-1]
end_loc = inlist[end_dep:]
else:
start_loc = inlist [start_dep:]
end_loc = inlist[end_dep: start_dep -1]
return start_loc, end_loc
directions = get_locations (l3) #change to l1 / l2 to see other outputs
print( "departure = " + str(directions[0]))
print( "arrival = " + str(directions[1]))
推荐阅读
- node.js - 如何从部署在 ZEIT Now 上的应用程序连接到 MongoDB 数据库?
- javascript - WebSocket 在 IE 上未定义,具体取决于浏览器 url。为什么?
- javascript - 在 React Native 中,每次按键时键盘都会消失
- reactjs - create-react-app:使用 --scripts-version 和 --typescript
- android - Android中带有动画设计的导航抽屉
- java - 创建只有一个段的 Lucene 索引
- javascript - 如何通过单击链接在 Angular 前端 Web 应用程序的浏览器中下载或保存 .csv 文件?浏览器投诉无文件
- javascript - 使用“让数组的元素”迭代时数组未更新
- google-analytics - Google Analytics 查询资源管理器:dateHourMinute 是标准时间还是本地时间?
- python-3.x - 将使用 for 循环生成的 QLineEdit 小部件中的值相加