[英]Get value from dictionary list value
I made a code where you create custom vehicles with license plates and info about them and I use dictionaries to keep track of them, and lists to keep track of what each car contains.我编写了一个代码,您可以在其中创建带有车牌和有关它们的信息的自定义车辆,我使用字典来跟踪它们,并使用列表来跟踪每辆车包含的内容。 So the key in the dictionary becomes the license plate and the list of attributes of the car becomes the value.所以字典中的键变成了车牌,汽车的属性列表变成了值。 Now I need to get each value separately in a print from the list.现在我需要从列表中分别打印出每个值。
I tried calling for values in the list as seen below with [] after the value but it didn't seem to work.我尝试在值之后使用 [] 调用列表中的值,如下所示,但它似乎不起作用。 The While loop is now working, apart from before.除了以前,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]])))
I just got an invalid syntax, and I want to get the value of each car printed.我刚得到一个无效的语法,我想打印每辆车的价值。
You've got quite a bit going on here that's not quite right.你在这里发生了很多事情,这并不完全正确。 Personally, I'd organize the problem a little differently.就个人而言,我会以不同的方式组织问题。
First, you don't need to call str()
in the result of input()
--it's already a string.首先,您不需要在input()
的结果中调用str()
) ——它已经是一个字符串。
Second, you're call to .upper
in the while loop is missing parens--it should be .upper()
.其次,您在 while 循环中调用.upper
缺少括号 - 它应该是.upper()
。 Also the conditional for the while loop is not correct. while 循环的条件也不正确。 car in carList
does return a boolean ( True
or False
) and Python will allow comparison of them to 1
and 0
, so that parts okay but not really the idiomatic way of writing it. car in carList
中的 car 确实返回 boolean ( True
或False
)和 Python 将允许将它们与1
和0
进行比较,因此部分可以,但不是真正的惯用写法。 You'd typically say car not in carList
and drop the == 0
portion.您通常会说car not in carList
并删除== 0
部分。 Also, car != 0
will always be true because if the user types 0
at the prompt, you actually get back the string '0'
which is not equal to the integer 0
.此外, car != 0
将始终为真,因为如果用户在提示符下键入0
,您实际上会返回不等于 integer 0
的字符串'0'
。
Finally, the way you're trying to pull out the data for a specific car in carList
is wrong.最后,您尝试在carList
中提取特定汽车的数据的方式是错误的。 Here's the snippet:这是片段:
carList[car[0], carList[car[1], carList[car[2]]
I can't really tell what the intention was here, but it's definitely a syntax problem.我无法真正说出这里的意图,但这绝对是一个语法问题。 You're missing a closing ]
in there at the very least, and depending on what you meant, you may not have enough arguments.您至少缺少一个结束]
,并且根据您的意思,您可能没有足够的 arguments。 It looks like you probably meant to write:看起来你可能打算写:
carList[car[0]], carList[car[1]], carList[car[2]]
In this case, you're trying to look up the vehicle by one character of the license plate.在这种情况下,您试图通过车牌的一个字符来查找车辆。 Substituting, you get:代入,你得到:
carList['A'], carList['B'], carList['C']
And it's pretty clear it's not what you want.很明显这不是你想要的。 Instead, you want to grab the list for car
.相反,您想获取car
的列表。 You get that by using the whole value of car
:您可以通过使用car
的全部价值来获得它:
carList[car]
That gets you the whole list.这让你得到了整个清单。 Now you want the individual elements, so you'd write:现在你想要单个元素,所以你会写:
carList[car][0], carList[car][1], carList[car][2]
Much better is to simply grab the list and stash it in a variable, and then use the new variable to get the data elements:更好的是简单地获取列表并将其存储在一个变量中,然后使用新变量来获取数据元素:
data = carList[car]
data[0], data[1], data[2]
In the end, I'd likely write something closer to this:最后,我可能会写一些更接近于这个的东西:
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]))
# ...
update : Based on your comment below, this could be helpful:更新:根据您在下面的评论,这可能会有所帮助:
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))
This is the final code I ended up with:这是我最终得到的最终代码:
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
The print:印刷品:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.