比较一个列表中的值

问题描述 投票:0回答:3

对于学校我有一个任务,但我不知道该怎么做。

我有两个电台,一个是startStation(开始)和一个eindStation(结束)。首先,我必须检查他们是否在车站列表中。这很好。然而,现在我必须检查是否在同一个列表中eindStation位于beginStation之后。

 stations_place = {"Schagen" : 1, "Heerhugowaard" : 2, "Alkmaar" : 3, "Castricum" : 4, "Zaandam" : 5, "Amsterdam Sloterdijk" : 6, "Amsterdam Centraal" : 7, "Amsterdam Amstel" : 8, "Utrecht Centraal" : 9, "'s-Hertogenbosch" : 10, "Eindhoven" : 11, "Weert" : 12, "Roermond" : 13, "Sittard" : 14, "Maastricht" : 15}

 eindStation = str(input("What is your end station? "))

 if eindStation in stations_place:
     print("good") #just to check if the code does it's job here
 else :
     print("This station isn't available, endstation is: Maastricht")

 if eindStation >= beginStation in stations_place.values:
     print("good") #just to check if the code does it's job here
 else:
     print("This station isn't available, endstation is: Maastricht")

我希望你们能帮助我。提前致谢!

python list
3个回答
2
投票

你需要先要求beginStation。 这是一种方式:

stations_place = {"Schagen" : 1, "Heerhugowaard" : 2, "Alkmaar" : 3, "Castricum" : 4, "Zaandam" : 5, "Amsterdam Sloterdijk" : 6, "Amsterdam Centraal" : 7, "Amsterdam Amstel" : 8, "Utrecht Centraal" : 9, "'s-Hertogenbosch" : 10, "Eindhoven" : 11, "Weert" : 12, "Roermond" : 13, "Sittard" : 14, "Maastricht" : 15}
eindStation = str(input("What is your end station? "))

if eindStation in stations_place:
    print("good") #just to check if the code does it's job here
else :
    print("This station isn't available, endstation is: Maastricht")
beginStation = str(input("What is your Starting station? "))
if stations_place[eindStation] >= stations_place[beginStation]:
    print("good") #just to check if the code does it's job here
else:
    print("This station isn't available, endstation is: Maastricht")

编辑:那个> =应该真的>因为没有人想从一个旅行到一个:)


0
投票

我想用户也要求使用beginStation,就像eindStation一样,对吧?

如果是,那么您可以进行第一次检查以检查beginStation。例如。:

if (eindStation in stations_place) and (beginStation in stations_place):

然后最后如果可能是:

if stations_place[eindStation] >= stations_place[beginStation]: 

希望这可以帮助。


0
投票

我不禁想到,在你的代码中将stations_place定义为list而不是dictionary会更好地用于你的目的。 list是“有序的”,而dictionary则不是。在您的情况下,站点是有序的,因为它们永远不会改变位置,因此选择有序的数据结构是有意义的。 如果您想扩展代码,它会让生活更轻松。 即

stations_place = ["Schagen","Heerhugowaard","Alkmaar","Castricum","Zaandam","Amsterdam Sloterdijk", "Amsterdam Centraal","Amsterdam Amstel","Utrecht Centraal","'s-Hertogenbosch","Eindhoven","Weert","Roermond","Sittard","Maastricht"]
result = False
while result == False:#Keep asking until a valid start station is input
    fromText =""
    for i in stations_place:#Build text station list
        fromText += i+" > "
    print (fromText+"\n")
    beginStation = str(input("What is your Starting station? "))
    if beginStation in stations_place:
        result = True
    else:
        print("This station isn't available, Starting station is:",stations_place[0],"\n")#first list item
result = False
while result == False:#Keep asking until a valid end station is input
    fromS = stations_place.index(beginStation)# Get index of start station
    fromText =""
    for i in stations_place[fromS:]:#Build text list of following stations
        fromText += i+" > "
    print (fromText+"\n")
    eindStation = str(input("What is your end station? "))
    if eindStation in stations_place:
        result = True
    else :
        print("This station isn't available, End station is:",stations_place[-1]+"\n")#Last list item

if stations_place.index(eindStation) > stations_place.index(beginStation):#Check index values
    print("Your journey is valid")
elif stations_place.index(eindStation) == stations_place.index(beginStation):#Check index values
    print("Your Start and End stations are the same")
else:
    print("Your end station is before the start station")
    print("Use the other platform for the other direction")

Schagen > Heerhugowaard > Alkmaar > Castricum > Zaandam > Amsterdam Sloterdijk > Amsterdam Centraal > Amsterdam Amstel > Utrecht Centraal > 's-Hertogenbosch > Eindhoven > Weert > Roermond > Sittard > Maastricht > 

What is your Starting station? Alkmaar
Alkmaar > Castricum > Zaandam > Amsterdam Sloterdijk > Amsterdam Centraal > Amsterdam Amstel > Utrecht Centraal > 's-Hertogenbosch > Eindhoven > Weert > Roermond > Sittard > Maastricht > 

What is your end station? Zaandam
Your journey is valid

作为旁注,Ruben是你的一个同学,因为这个问题的变体已经在SO Python trainticket machine,所以要注意,你的老师可以找到这个查询,因为它是我的搜索引擎中第一个带有查询的项目“python zaandam站”。

© www.soinside.com 2019 - 2024. All rights reserved.