Python:如何在Python中向列表列表添加列表?

Python:如何在Python中向列表列表添加列表?,python,Python,我正在学习python,所以这个问题可能是一个简单的问题,我正在创建一个汽车列表及其详细信息列表,如下所示: car_specs = [("1. Ford Fiesta - Studio", ["3", "54mpg", "Manual", "£9,995"]), ("2. Ford Focous - Studio", ["5", "48mpg", "Manual", "£17,295"]), ("3. Vauxhall Corsa STING

我正在学习python,所以这个问题可能是一个简单的问题,我正在创建一个汽车列表及其详细信息列表,如下所示:

car_specs = [("1. Ford Fiesta - Studio", ["3", "54mpg", "Manual", "£9,995"]),
             ("2. Ford Focous - Studio", ["5", "48mpg", "Manual", "£17,295"]),
             ("3. Vauxhall Corsa STING", ["3", "53mpg", "Manual", "£8,995"]),
             ("4. VW Golf - S", ["5", "88mpg", "Manual", "£17,175"])
            ]
然后,我创建了一个用于添加另一辆车的零件,如下所示:

new_name = input("What is the name of the new car?")
new_doors = input("How many doors does it have?")
new_efficency = input("What is the fuel efficency of the new car?")
new_gearbox = input("What type of gearbox?")
new_price = input("How much does the new car cost?")
car_specs.insert(len(car_specs), (new_name[new_doors, new_efficency, new_gearbox, new_price]))
但它不起作用,出现以下错误:

Would you like to add a new car?(Y/N)Y
What is the name of the new car?test
How many doors does it have?123456
What is the fuel efficency of the new car?23456
What type of gearbox?234567
How much does the new car cost?234567
Traceback (most recent call last):
  File "/Users/JagoStrong-Wright/Documents/School Work/Computer Science/car list.py", line 35, in <module>
    car_specs.insert(len(car_specs), (new_name[new_doors, new_efficency, new_gearbox, new_price]))
TypeError: string indices must be integers
>>> 
是否要添加新车?(是/否)是
这辆新车叫什么名字?测试
有多少扇门?123456
这辆新车的燃油效率是多少?23456
什么类型的变速箱?234567
这辆新车多少钱?234567
回溯(最近一次呼叫最后一次):
文件“/Users/JagoStrong-Wright/Documents/School Work/Computer Science/car list.py”,第35行,在
插入汽车规格(透镜(汽车规格),(新名称[新车门、新效率、新变速箱、新价格])
TypeError:字符串索引必须是整数
>>> 

非常感谢任何人的帮助。

您没有正确设置元组中的第一个元素。正如您所期望的,您正在将名称附加到汽车规格的长度

另外,new_name是一个字符串,当您执行new_name[x]时,您会向python询问该字符串中的x+1个字符

new_name = input("What is the name of the new car?")
new_doors = input("How many doors does it have?")
new_efficency = input("What is the fuel efficency of the new car?")
new_gearbox = input("What type of gearbox?")
new_price = input("How much does the new car cost?")
car_specs.insert(str(len(car_specs + 1))+'. - ' + name, [new_doors, new_efficency, new_gearbox, new_price])

只需将元组附加到列表中,确保用
将新名称与列表分开:

new_name = input("What is the name of the new car?")
new_doors = input("How many doors does it have?")
new_efficency = input("What is the fuel efficency of the new car?")
new_gearbox = input("What type of gearbox?")
new_price = input("How much does the new car cost?")
car_specs.append(("{}. {}".format(len(car_specs) + 1,new_name),[new_doors, new_efficency, new_gearbox, new_price]))
我将使用dict来存储数据:

car_specs = {'2. Ford Focous - Studio': ['5', '48mpg', 'Manual', '\xc2\xa317,295'], '1. Ford Fiesta - Studio': ['3', '54mpg', 'Manual', '\xc2\xa39,995'], '3. Vauxhall Corsa STING': ['3', '53mpg', 'Manual', '\xc2\xa38,995'], '4. VW Golf - S': ['5', '88mpg', 'Manual', '\xc2\xa317,175']}
然后使用以下方法添加新车:

car_specs["{}. {}".format(len(car_specs)+1,new_name)] = [new_doors, new_efficency, new_gearbox, new_price]

您没有在新名称后面加逗号,这使其成为一个切片操作。(新名称,[新车门,新效率,新变速箱,新价格])谢谢您的帮助:)