如何使用用户inpu附加到2D列表

2024-05-23 22:07:33 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我的列表是

superheroes = [["Superman", "flying"], 
               ["Spiderman", "web"], 
               ["Batman", "Technology"], 
               ["Wonder woman","Lasso of Truth"]] 

Tags: ofweb列表wondertruthlassospidermanflying
2条回答

就像你附加到任何列表一样:

superheroes += [["Green lantern","A green lantern..."]]

或者

superheroes.append(["Green lantern","A green lantern..."])

你可以这样做

temp = []
name = input("Enter Superhero's name")
temp.append(name)
type = input("Enter Superhero's type")
temp.append(type)
superheroes.append(temp)

这将要求用户输入超级英雄的名字和类型,并在现有列表中添加一个列表。

另一种方法是在一行中确定用户输入并更新列表:

superheroes.append([input("Enter superhero's name"),input("Enter superhero's type")])

相关问题 更多 >