Home > database >  Python - combination of "AND" and "NOT IN" won't work
Python - combination of "AND" and "NOT IN" won't work

Time:08-24

I'm currently practicing on recursion and growing lists. However, I encountered an error where both "AND" and "NOT IN" functions won't work. It keeps on returning True regardless of the outcome. Based on the code, this line: if (a not in routeList) and (b not in routeList): won't work.

Here's my Input and Output:

You are currently in LGA airport.

Here are the airports ['BIG', 'CDG', 'GGG', 'DEL', 'DOH', 'DSM', 'EWR', 'EYW', 'HND', 'ICN', 'JFK', 'LGA', 'LHR', 'ORD', 'SAN', 'SFO', 'SIN', 'TLV', 'BUD']

-What's your destination? SIN

count: 0

a CDG

routeList [['CDG']]

count: 1

a FFF

routeList [['CDG'], ['FFF']]

count: 2

b CDG

routeList [['CDG'], ['FFF'], ['CDG']]

The last array ['CDG'] should not be included on the current routeList because it already exists. How can I fix this?

#You must return the shortest path in any airports.
#The starting Airport should be in LGA

#List of Airports
airports = ['BIG', 'CDG', 'GGG', 'DEL', 'DOH', 'DSM', 'EWR', 'EYW', 'HND', 'ICN', 'JFK', 'LGA', 'LHR', 'ORD', 'SAN', 'SFO', 'SIN', 'TLV', 'BUD']

#This is the airport routes
routes = [
    ['BGI', 'LGA'],
    ['CDG', 'BUD'],
    ['CDG', 'SIN'],
    ['DEL', 'CDG'],
    ['DEL', 'DOH'],
    ['DSM', 'ORD'],
    ['EWR', 'HND'],
    ['EYW', 'LHR'],
    ['FFF', 'SIN'],
    ['HND', 'ICN'],
    ['HND', 'JFK'],
    ['ICN', 'JFK'],
    ['JFK', 'LGA'],
    ['LHR', 'SFO'],
    ['ORD', 'BGI'],
    ['SAN', 'EYW'],
    ['SFO', 'DSM'],
    ['SFO', 'SAN'],
    ['SIN', 'CDG'],
    ['TLV', 'DEL'],
    ]

startingAirport = 'LGA'

routeList = []
tempList = []


def checkRoute(startPt):
    global routes
    global routeList
    global startingAirport

    tempList = []

    
    count1 = 0
    for i in routes:
        if startPt in i:
            a,b = i
            
            if (a not in routeList) and (b not in routeList):# == True:
            #if (a,b) not in routeList:
            
            
                print('count: %d' %count1)
                if (a != startPt):# and (a not in routeList):
                    tempList.append(a)
                    print('a', a)
                elif b != startPt:# and (b not in routeList):
                    tempList.append(b)
                    print('b', b)
                count1 =1

            routeList.append(tempList[:])
            print('routeList',routeList)
            tempList.clear()
            
def main():
    print(len(routes))
    print("You are currently in %s airport.\n" % startingAirport)
    print("Here are the airports")
    print(airports)
    userIn = input("What's your destination? ")
    if userIn not in airports:
        print("\nThis Airport name is not listed. Please try again")
        main()
    else:
        checkRoute(userIn)

main()

CodePudding user response:

Your problem is that the string 'CDG' does not equal the array ['CDG']

It's a little messy but I trust you can clean it up:

        if ([a] not in routeList) and ([b] not in routeList):
            count1 =1
            print('count: %d' %count1)
            if (a != startPt):
                tempList.append(a)
                print('a', a)
            elif b != startPt:
                tempList.append(b)
                print('b', b)
            routeList.append(tempList[:])
            print('routeList',routeList)
        tempList.clear()
  • Related