简体   繁体   English

Python:如何在python的列表列表中添加列表?

[英]Python: How do you add a list to a list of lists in python?

I am learning python so this question may be a simple question, I am creating a list of cars and their details in a list as bellow: 我正在学习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"])
            ]

I have then created a part for adding another car as follows: 然后,我创建了一个零件来添加另一辆车,如下所示:

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]))

It isnt working though and comes up with this error: 它不起作用,但出现此错误:

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
>>> 

Anyones help would be greatly appreciated, thanks. 任何人的帮助将不胜感激,谢谢。

Just append the tuple to the list making sure to separate new_name from the list with a , : 只需将元组附加到列表中,并确保将new_name与列表中的,分开:

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]))

I would use a dict to store the data instead: 我将使用字典来存储数据:

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']}

Then add new cars using: 然后使用以下方法添加新车:

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

You are not setting the first element go your tuple correctly. 您没有设置第一个元素正确地进入元组。 You are appending the name to the length of car specs as you expect. 您将按预期将名称附加在汽车规格的长度上。

Also new_name is as string, when you do new_name[x] your asking python for the x+1th character in that string. 同样,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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM