如何在python中找到数组中列表项的.index?

时间:2017-02-02 01:44:29

标签: python arrays

我在一个奇异数组中有多个列表数,并且想要找出数组中某个列表的位置。我希望这个位置的列表必须包含用户输入的单词作为该列表的第一项。

john = ['John', 'Smith', 'm', 34, 1.7, True]
mary = ['Mary', 'Smith', 'f', 33, 1.54, False]
frank = ['Frank', 'Lee', 'm', 48, 1.83, False]
mark = ['Mark', 'Abbott', 'm', 27, 1.73, True]
jasmine = ['Jasmine', 'Healy', 'f', 19, 1.64, True]
cathy = ['Cathy', 'Potter', 'f', 19, 1.59, True]

LIST = [john, mary, frank, mark, jasmine, cathy]

print("Hello, welcome to the parachutist searcher.")
loop = 0
while loop == 0:
    name = input("Please enter a first name: ")
    if name in [item[0] for item in LIST]:
        finalName = name
        print(finalName)
        position = LIST.index([finalName])
        print(position)
    else:
        print("Nup.")

由于某种原因,当我尝试打印位置时,它返回它无法在数组中找到finalName。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您总是可以使用简单的for语句。像这样格式化也不会进入无限循环。

name = input("Give me a name.")
found = false
for each in LIST:
    if name in each:
        found = true
        print(name)
        print(LIST.index(each))
if found == false:
    print("nup.")