首页 > 解决方案 > 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!

标签: pythonlistidentification

解决方案


这应该适合你:

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]))

推荐阅读