【发布时间】:2016-05-02 10:37:03
【问题描述】:
为简单起见,假设我使用如下列表:
[['Bob', 'Pizza', 'Male'], ['Sally', 'Tacos', 'Female']]
我想询问用户他们想查看哪个人的统计信息,以便在调用时打印出Bob、Pizza 和Male。我尝试使用 index 方法,但我正在使用的列表列表中有超过 150 个条目。
我尝试使用类似的东西:
personName = input("Enter the person whose stats you would like to see: )
personIndex = personList.index(personName)
personStats = personList[personName][1:3] # first index is the name, index 1 and 2 is favorite food and gender
print(personStats)
但它不起作用。
【问题讨论】:
-
你应该使用 dict() 其中名称是键