繁体   English   中英

如何在 python 的列表中查找项目索引

[英]How to find index of item in list in python

I am techy101 and I want to know How to find the index of an item in a list in python, I have tried the .index function before but that didn't give me the output I wanted, I have tried the enumerate() function同样,它也没有给出好的 output,这是我的代码:

alphabet = list("abcdefghijklmnopqrstuvwxyz")
message = list(input("enter message (only words not numbers):"))
key = int(input("enter key: "))
w = 0
compile_list = []
for eggsample in range(len(message)):
    print(f"thingy: {message[w]}")
    
    print(f"index is {message.index(message[w])}")
    number = message.index(message[w]) + key
    print(number)
    _ = alphabet[number]
    print(_)
    compile_list.append(_)
    w+=1
print("your new message is: ")
print("".join(compile_list))

它给了我这个:

thingy: h
index is 0
2
c
thingy: e
index is 1
3
d
thingy: l
index is 2
4
e
thingy: l
index is 2
4
e
thingy: o
index is 4
6
g

有谁知道如何实际找到该项目的索引?

尝试改进第 8 行的代码如下

print(f "index is {alphabet.index(message[w])}")

如果要检查“字母”列表而不是“消息”列表中的索引,可以使用 alphabet.index(value)。

暂无
暂无

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

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