简体   繁体   中英

How to find an element in a list from its position(which i found using index()) in python

alpha = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

that is the list

for letters in sentence:
    pos2 = alpha.index(letters) + 1
    #to find the positions of each letter
    new_sentence.append(pos2)
    #to add each position to the empty list
print (new_sentence)

that is what i used to find the positions of each letter in the inputted message in the alphabet list

now i wish to convert it back to the letters from positions..

You can index it:

[alpha[p-1] for p in new_sentence]

sentence = "helloworld"
# ... run your code to get the new_sentence
''.join(alpha[p-1] for p in new_sentence)

# 'helloworld'

If you are intending to find the letter after the original letter, you can take the remainder of the index as from comment @RushyPanchal:

sentence = "hello world"

# ... run your code to get the new_sentence
''.join(alpha[p % len(alpha)] for p in new_sentence)

# 'ifmmpxpsme'

Because you have the index you can grab the value at that index.

print my_list[pos2]

python also has a built in method enumerate(enumerable_obj) that returns index, value pairs

for index, value in enumerate(my_list):
    print index, value
alpha_text = ""
for i in new_sentence:
    alpha_text = alpha_text + alpha[i - 1]
print(alpha_text)

So your whole code would look like:

sentence = "lololololololololol" #your text
alpha = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
new_sentence = []
for letters in sentence:
    pos2 = alpha.index(letters) + 1
    #to find the positions of each letter
    new_sentence.append(pos2)

print (new_sentence)
alpha_text =""
for i in new_sentence:
    alpha_text = alpha_text + alpha[i - 1]
print(alpha_text)

output:

 [12, 15, 12, 15, 12, 15, 12, 15, 12, 15, 12, 15, 12, 15, 12, 15, 12, 15, 12]

 lololololololololol

@Psidom's answer is the correct way to go about getting a list of characters from the string.

However, if you want to just shift the characters, you can use the chr and ord functions:

sentence = "some string"
shifted_sentence = ''.join(chr(ord(c)+1) for c in sentence)

In terms of performance, it should be easier to make a Dictionary of letters and their values and vice-versa. This way you only use a constant time for each look-up. This change makes your code much more scalable and faster.

An approach working with enumerate() and filter() :

>>> sentence = 'hello'

For this example is:

>>> new_sentence
[8, 5, 12, 12, 15]

The result is as follows:

>>> letters_enum = [(j,c) for j,c in enumerate(alpha, 1) if j in new_sentence]
>>> result = []
>>> for i in new_sentence:
...     letter = list(filter(lambda item: item[0] == i, letters_enum))
...     result.extend(letter[0][1]*len(letter))
...
>>>
>>> result
['h', 'e', 'l', 'l', 'o']

Your current approach is inefficient, because you have to search through up to 26 different list items for every letter in your input sentence. It also fails for the letter "z", since there's nothing after "z" in the list.

Since you've clarified that you're trying to do a keyword cipher , a more efficient method would be to use str.translate :

import string

keyword = 'stack'
source = string.ascii_lowercase
target = keyword + ''.join(filter(lambda x: x not in keyword, source))

sentence = 'encode me'
encoded = sentence.translate(str.maketrans(source, target))

print(encoded)

Output:

klamck jk

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