Python 从字典列表值中获取值
我制作了一个代码,你可以创建带有车牌和相关信息的定制车辆,我使用字典来跟踪它们,使用列表来跟踪每辆车包含的内容。 因此,字典中的钥匙成为车牌,汽车属性列表成为值。现在,我需要从列表中分别打印每个值 我尝试调用列表中的值,如下所示,在值后面加上[] 但它似乎不起作用。除了以前,While循环现在正在工作Python 从字典列表值中获取值,python,dictionary,Python,Dictionary,我制作了一个代码,你可以创建带有车牌和相关信息的定制车辆,我使用字典来跟踪它们,使用列表来跟踪每辆车包含的内容。 因此,字典中的钥匙成为车牌,汽车属性列表成为值。现在,我需要从列表中分别打印每个值 我尝试调用列表中的值,如下所示,在值后面加上[] 但它似乎不起作用。除了以前,While循环现在正在工作 carList = {"ABC 123": ["Mustang GT", 1969, "Black"]} car = str(input("What car would you like to w
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]])))
我只是得到了一个无效的语法,我想打印每辆车的值。这里有很多不太正确的地方。就我个人而言,我会以稍微不同的方式组织这个问题 首先,您不需要在
input()
的结果中调用str()
——它已经是一个字符串了
其次,while循环中对.upper
的调用缺少参数——它应该是.upper()
。while循环的条件也不正确True
或False
),Python将允许将它们与1
和0
进行比较,这样部分就可以了,但不是真正惯用的编写方式。您通常会说car not in carList
,然后删除==0
部分。还有,car!=0
将始终为真,因为如果用户在提示下键入0
,实际上会返回字符串'0'
,该字符串不等于整数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
的整个值得到:
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))
这是我最后的代码:
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
检查一辆车是否在字典中未使用,而在carList中未使用,即我自己解决了其中的几个问题,例如我如何错过在参数末尾添加]。但是带[0]的额外[]非常有用。它现在起作用了!这对于未来的项目来说是非常好的。问题是,我应该使用类和对象,但我真的不知道如何创建新变量并使用我收集的数据分配它们,除非我手动创建一个新变量,所以我能想到的唯一解决方法是使用带有列表的值的字典来解决这个问题,我认为结果很棒!不客气!我用一种可能的合并类和对象的方法更新了答案。我真的不知道你的作业,所以很难说这是不是正确的方法,但至少有一些片段可以指引方向。我在下面发布了我自己的解决方案。很好用!整个代码在repl中: