简体   繁体   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 too, again it didn't give a good output, here is my code: 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))

and it gives me this:它给了我这个:

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

Does anyone know how to actually find the index of the item?有谁知道如何实际找到该项目的索引?

Try to improve the code in line 8 as follows尝试改进第 8 行的代码如下

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

If you want to check the index in a list of "alphabets" instead of a list of "messages", you can use alphabet.index(value).如果要检查“字母”列表而不是“消息”列表中的索引,可以使用 alphabet.index(value)。

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

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