【发布时间】:2018-08-19 16:40:32
【问题描述】:
我怎样才能在下面的程序中得到这个?:
dict_cars {1 : {'Mercedes':'E500'}},{ 2 : {'Ford' : 'Focus'}},{ 3 {'Toyota' : 'Celica'}}
我当前的程序不工作,我不知道如何解决它:(
dict_cars = {}
attributes = {}
car_number = input ('Insert car number: ')
car_brand = input ('Insert car brand: ')
car_model = input ('Insert car model: ')
while car_number != 'end':
dict_cars[car_number] = attributes
dict_cars[car_number][car_brand] = car_model
car_number = input ('Insert car number: ')
car_brand = input ('Insert car brand: ')
car_model = input ('Insert car model: ')
我得到的是我想要的:
Insert car number: 1
Insert car brand: Mercedes
Insert car model: E500
Insert car number: 2
Insert car brand: Ford
Insert car model: Focus
Insert car number: 3
Insert car brand: Toyota
Insert car model: Celica
Insert car number: end
Insert car brand:
Insert car model:
>>> dict_cars
{'1': {'Mercedes': 'E500', 'Ford': 'Focus', 'Toyota': 'Celica'}, '2'{'Mercedes': 'E500', 'Ford': 'Focus', 'Toyota': 'Celica'}, '3': {'Mercedes':
'E500', 'Ford': 'Focus', 'Toyota': 'Celica'}}
【问题讨论】:
-
如果您的车号始终采用类似的升序排列,您可能需要考虑使用字典列表而不是字典字典。它使索引更简单
标签: python