简体   繁体   English

如何在python中从位置找到列表中的元素(我使用index()找到)

[英]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: 如果您打算在原始字母之后找到该字母,则可以使用索引@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 python还有一个内置方法enumerate(enumerable_obj)返回index, value

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. @Psidom的答案是从字符串中获取字符列表的正确方法。

However, if you want to just shift the characters, you can use the chr and ord functions: 但是,如果只想移动字符,可以使用chrord函数:

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() : 使用enumerate()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. 您当前的方法效率低下,因为您必须为输入句子中的每个字母搜索多达26个不同的列表项。 It also fails for the letter "z", since there's nothing after "z" in the list. 字母“ z”也将失败,因为列表中“ z”之后没有任何内容。

Since you've clarified that you're trying to do a keyword cipher , a more efficient method would be to use str.translate : 既然您已经阐明要尝试使用关键字cipher ,那么更有效的方法是使用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

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

相关问题 我可以根据用户使用python在列表中的位置向用户提供从列表中选择元素的选项吗? - Can I give the user an option to chose an element from the list based on its position in the list using python? 如何在Python中该列表的子列表中最小的列表中找到元素的索引? - How to find the index of an element in a list which is minimum in a sublist of that list in Python? 如何从列表及其索引中找到最大元素? - How to find maximum element from a list and its index? 如何在 python 中找到列表的排序位置/索引? - How do I find the sorted position/index of a list in python? 如何使用变量在 Python 的列表中查找元素的 position? - How do I use a variable to find the position of an element in a list in Python? 如何从链表中找到第一个索引,在 Python 中使用递归找到某个值 - How to find the first index from the linked list, where the certain value is found using recursion in Python Python:如何按部分查找列表中的索引? - Python: How find the index in list by its part? 如何通过列表在Python中的位置识别龟? - How can i identify a turtle from a list by its position in Python? 如何在 Python 中找到列表中元素的位置? - How to find the position of an element in a list , in Python? 返回在列表中首次找到元素的索引-Python - Return the index at which an element is first found in a list - Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM