简体   繁体   中英

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:

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

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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