繁体   English   中英

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

[英]How to find .index of list item within array on python?

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

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。 我该如何解决?

你总是可以使用一个简单的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.")

暂无
暂无

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

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