从字典列表中获取价值

时间:2019-10-19 09:38:00

标签: python dictionary

我编写了一个代码,您可以在其中创建带有车牌和有关其信息的自定义车辆,并使用字典来跟踪它们,并使用列表来跟踪每辆车包含的内容。 因此,词典中的键成为车牌,汽车的属性列表成为值。现在,我需要从列表中单独获取每个值。

我尝试在列表中调用值,如下所示,在值后使用[] 但它似乎没有用。 While循环现在可以正常工作了。

carList = {"ABC 123": ["Mustang GT", 1969, "Black"]}
car = str(input("What car would you like to withdraw? Write the license plate")).upper()

car = str(input("What car would you like to withdraw? Write the license plate: ")).upper()

while (car in carList.keys()) == 0 and car != "0":
  car = str(input("That car doesen't exist. Write a new one or press 0: ")).upper()

choice = str(input("You wish to withdraw {}\nModel: {}\nYear: {}\nColour: {}".format(car, carList[car[0]], carList[car[1]], carList[car[2]])))

我刚得到一个无效的语法,我想打印出每辆汽车的价格。

2 个答案:

答案 0 :(得分:1)

您在这里进行了很多操作,这不太正确。就个人而言,我对问题的组织方式有所不同。

首先,您不需要在str()的结果中调用input(),因为它已经是字符串。

其次,您在while循环中缺少括号的情况下调用了.upper -应该为.upper()。而且while循环的条件不正确。 car in carList确实返回了布尔值(TrueFalse),Python允许将它们与10进行比较,因此部分内容还可以,但实际上不是惯用的书写方式。您通常会说car not in carList并丢下== 0部分。另外,car != 0始终为真,因为如果用户在提示符下键入0,您实际上会返回字符串'0',该字符串 not 等于整数0

最后,您尝试提取carList中特定汽车的数据的方法是错误的。这是代码段:

carList[car[0], carList[car[1], carList[car[2]]

我不能真正说出这里的意图,但这绝对是一个语法问题。您至少在其中缺少结尾],并且根据您的意思,您可能没有足够的参数。看来您可能打算写:

carList[car[0]], carList[car[1]], carList[car[2]]

在这种情况下,您试图按车牌的一个字符查找车辆。替换为:

carList['A'], carList['B'], carList['C']

很明显,这不是您想要的。相反,您想获取car的列表。您可以通过使用car的整个值来实现:

carList[car]

这使您获得整个列表。现在,您需要各个元素,因此您可以编写:

carList[car][0], carList[car][1], carList[car][2]

更好的方法是简单地获取列表并将其存储在变量中,然后使用新变量获取数据元素:

data = carList[car]
data[0], data[1], data[2]

最后,我可能会写得更近一些:

carList = {"ABC 123": ["Mustang GT", 1969, "Black"]}

while True:
    car = input("What car would you like to withdraw? Write the license plate: ").upper()

    if car == '0':
        break

    if car not in carList:
        print("That car doesn't exist. Press 0 to abort or write a new license plate.")
        continue

    data = carList[car]

    choice = input("You wish to withdraw {}\nModel: {}\nYear: {}\nColour: {}\n: ".format(
        car, data[0], data[1], data[2]))

    # ...

更新:根据您在下面的评论,这可能会有所帮助:

class Car:
    def __init__(self, model, year, color):
        self.model = model
        self.year = year
        self.color = color

carList = {"ABC 123": Car("Mustang GT", 1969, "Black")}

# ...

    choice = input("You wish to withdraw {}\nModel: {}\nYear: {}\nColour: {}\n: ".format(
    car, data.model, data.year, data.color))

答案 1 :(得分:1)

这是我最后得到的最终代码:

carList = {"ABC 123": ["Mustang GT", 1969, "Black"]}
while True:
  car = str(input("What car would you like to withdraw? Write the license plate: ")).upper()
  while (car in carList.keys()) == 0 and car != "0":
    car = str(input("That car doesen't exist. Write a new one or press 0: ")).upper()
  if car == "0":
    break
  choice = str(input("You wish to withdraw {}\nModel: {}\nYear: {}\nColour: {}".format(car, carList[car][0], carList[car][1], carList[car][2])))
  a = input("Press enter to continue: ")
       break

打印:

What car would you like to withdraw? Write the license plate: abc 123
You wish to withdraw ABC 123
Model: Mustang GT
Year: 1969
Colour: Black