Zahra Haider Bilal
Zahra Haider Bilal

Reputation: 3

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

Upvotes: 0

Views: 176

Answers (7)

ThisSuitIsBlackNot
ThisSuitIsBlackNot

Reputation: 24063

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

Upvotes: -1

ettanany
ettanany

Reputation: 19806

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']

Upvotes: 0

Taufiq Rahman
Taufiq Rahman

Reputation: 5704

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

Upvotes: 1

Shobeir
Shobeir

Reputation: 127

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.

Upvotes: 0

TheLazyScripter
TheLazyScripter

Reputation: 2665

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

Upvotes: 1

akuiper
akuiper

Reputation: 214927

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'

Upvotes: 4

Rushy Panchal
Rushy Panchal

Reputation: 17532

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

Upvotes: 1

Related Questions