Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于其他值返回python列表中索引的值_Python - Fatal编程技术网

基于其他值返回python列表中索引的值

基于其他值返回python列表中索引的值,python,Python,我已经把字母a-z列在一张单子上了。如何根据用户键入的内容在列表中查找项目的值 例如,如果他们键入字母a,它将返回c,f将返回h,x将返回z letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'] newletters = [] offset = 2 userInput = in

我已经把字母a-z列在一张单子上了。如何根据用户键入的内容在列表中查找项目的值

例如,如果他们键入字母
a
,它将返回
c
f
将返回
h
x
将返回
z

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

offset = 2

userInput = input('type a string')
newvalue = chr(ord(userInput)+offset)
split = list(newvalue)
print split
以上内容适用于字符,但不适用于字符串..帮助

您可以尝试以下方法:

>>> offset = 2
>>> aString = raw_input("digit a letter: ")
>>> aString
'a'
>>> chr(ord(aString)+offset)
'c'
文件:


如果要迭代整个字符串,一种简单的方法是使用
for
循环。我假设输入字符串总是小写的

EDIT2:我改进了解决方案,以处理字母为
'y'
'z'
且没有“旋转”的情况,例如:

# with only offset addiction this return a non-alphabetic character
>>> chr(ord('z')+2)
'|'

# the 'z' rotation return the letter 'b'
>>> letter = "z"
>>> ord_letter = ord(letter)+offset
>>> ord_letter_rotated = ((ord_letter - 97) % 26) + 97
>>> chr(ord_letter_rotated)
'b'
代码解决方案:

offset = 2
aString = raw_input("digit the string to convert: ")
#aString = "abz"
newString = ""

for letter in aString:
    ord_letter = ord(letter)+offset
    ord_letter_rotated = ((ord_letter - 97) % 26) + 97
    newString += chr(ord_letter_rotated)

print newString
整个小写字母表的此代码输出:

cdefghijklmnopqrstuvwxyzab
注意:您也可以通过以下方式免费获得小写字母:

>>> import string
>>> string.lowercase
'abcdefghijklmnopqrstuvwxyz'
查看维基百科页面了解有关ROT13的信息:

您可以尝试以下方法:

>>> offset = 2
>>> aString = raw_input("digit a letter: ")
>>> aString
'a'
>>> chr(ord(aString)+offset)
'c'
文件:


如果要迭代整个字符串,一种简单的方法是使用
for
循环。我假设输入字符串总是小写的

EDIT2:我改进了解决方案,以处理字母为
'y'
'z'
且没有“旋转”的情况,例如:

# with only offset addiction this return a non-alphabetic character
>>> chr(ord('z')+2)
'|'

# the 'z' rotation return the letter 'b'
>>> letter = "z"
>>> ord_letter = ord(letter)+offset
>>> ord_letter_rotated = ((ord_letter - 97) % 26) + 97
>>> chr(ord_letter_rotated)
'b'
代码解决方案:

offset = 2
aString = raw_input("digit the string to convert: ")
#aString = "abz"
newString = ""

for letter in aString:
    ord_letter = ord(letter)+offset
    ord_letter_rotated = ((ord_letter - 97) % 26) + 97
    newString += chr(ord_letter_rotated)

print newString
整个小写字母表的此代码输出:

cdefghijklmnopqrstuvwxyzab
注意:您也可以通过以下方式免费获得小写字母:

>>> import string
>>> string.lowercase
'abcdefghijklmnopqrstuvwxyz'
查看维基百科页面了解有关ROT13的信息:

您可以尝试以下方法:

>>> offset = 2
>>> aString = raw_input("digit a letter: ")
>>> aString
'a'
>>> chr(ord(aString)+offset)
'c'
文件:


如果要迭代整个字符串,一种简单的方法是使用
for
循环。我假设输入字符串总是小写的

EDIT2:我改进了解决方案,以处理字母为
'y'
'z'
且没有“旋转”的情况,例如:

# with only offset addiction this return a non-alphabetic character
>>> chr(ord('z')+2)
'|'

# the 'z' rotation return the letter 'b'
>>> letter = "z"
>>> ord_letter = ord(letter)+offset
>>> ord_letter_rotated = ((ord_letter - 97) % 26) + 97
>>> chr(ord_letter_rotated)
'b'
代码解决方案:

offset = 2
aString = raw_input("digit the string to convert: ")
#aString = "abz"
newString = ""

for letter in aString:
    ord_letter = ord(letter)+offset
    ord_letter_rotated = ((ord_letter - 97) % 26) + 97
    newString += chr(ord_letter_rotated)

print newString
整个小写字母表的此代码输出:

cdefghijklmnopqrstuvwxyzab
注意:您也可以通过以下方式免费获得小写字母:

>>> import string
>>> string.lowercase
'abcdefghijklmnopqrstuvwxyz'
查看维基百科页面了解有关ROT13的信息:

您可以尝试以下方法:

>>> offset = 2
>>> aString = raw_input("digit a letter: ")
>>> aString
'a'
>>> chr(ord(aString)+offset)
'c'
文件:


如果要迭代整个字符串,一种简单的方法是使用
for
循环。我假设输入字符串总是小写的

EDIT2:我改进了解决方案,以处理字母为
'y'
'z'
且没有“旋转”的情况,例如:

# with only offset addiction this return a non-alphabetic character
>>> chr(ord('z')+2)
'|'

# the 'z' rotation return the letter 'b'
>>> letter = "z"
>>> ord_letter = ord(letter)+offset
>>> ord_letter_rotated = ((ord_letter - 97) % 26) + 97
>>> chr(ord_letter_rotated)
'b'
代码解决方案:

offset = 2
aString = raw_input("digit the string to convert: ")
#aString = "abz"
newString = ""

for letter in aString:
    ord_letter = ord(letter)+offset
    ord_letter_rotated = ((ord_letter - 97) % 26) + 97
    newString += chr(ord_letter_rotated)

print newString
整个小写字母表的此代码输出:

cdefghijklmnopqrstuvwxyzab
注意:您也可以通过以下方式免费获得小写字母:

>>> import string
>>> string.lowercase
'abcdefghijklmnopqrstuvwxyz'
查看维基百科页面了解有关ROT13的信息:


对于
z
应该发生什么?是否应该变成
b

您可以使用Python的和函数来执行以下操作:

import string

def rotate(text, by):
    s_from = string.ascii_lowercase
    s_to = string.ascii_lowercase[by:] + string.ascii_lowercase[:by]
    cypher_table = string.maketrans(s_from, s_to)
    return text.translate(cypher_table)

user_input = raw_input('type a string: ').lower()
print rotate(user_input, 2)
type a string: abcxyz
cdezab
abcdefghijklmnopqrstuvwxyz
cdefghijklmnopqrstuvwxyzab
这对整个字符串起作用,如下所示:

import string

def rotate(text, by):
    s_from = string.ascii_lowercase
    s_to = string.ascii_lowercase[by:] + string.ascii_lowercase[:by]
    cypher_table = string.maketrans(s_from, s_to)
    return text.translate(cypher_table)

user_input = raw_input('type a string: ').lower()
print rotate(user_input, 2)
type a string: abcxyz
cdezab
abcdefghijklmnopqrstuvwxyz
cdefghijklmnopqrstuvwxyzab
它是如何工作的?

如果从打印
s_,并将
s_打印到
中,它们看起来如下所示:

import string

def rotate(text, by):
    s_from = string.ascii_lowercase
    s_to = string.ascii_lowercase[by:] + string.ascii_lowercase[:by]
    cypher_table = string.maketrans(s_from, s_to)
    return text.translate(cypher_table)

user_input = raw_input('type a string: ').lower()
print rotate(user_input, 2)
type a string: abcxyz
cdezab
abcdefghijklmnopqrstuvwxyz
cdefghijklmnopqrstuvwxyzab

maketrans
创建一个映射表,将
s\u中的字符从
映射到
s\u到
translate
然后将此映射应用于字符串。

对于
z
应该发生什么?是否应该变成
b

您可以使用Python的和函数来执行以下操作:

import string

def rotate(text, by):
    s_from = string.ascii_lowercase
    s_to = string.ascii_lowercase[by:] + string.ascii_lowercase[:by]
    cypher_table = string.maketrans(s_from, s_to)
    return text.translate(cypher_table)

user_input = raw_input('type a string: ').lower()
print rotate(user_input, 2)
type a string: abcxyz
cdezab
abcdefghijklmnopqrstuvwxyz
cdefghijklmnopqrstuvwxyzab
这对整个字符串起作用,如下所示:

import string

def rotate(text, by):
    s_from = string.ascii_lowercase
    s_to = string.ascii_lowercase[by:] + string.ascii_lowercase[:by]
    cypher_table = string.maketrans(s_from, s_to)
    return text.translate(cypher_table)

user_input = raw_input('type a string: ').lower()
print rotate(user_input, 2)
type a string: abcxyz
cdezab
abcdefghijklmnopqrstuvwxyz
cdefghijklmnopqrstuvwxyzab
它是如何工作的?

如果从
打印
s_,并将
s_打印到
中,它们看起来如下所示:

import string

def rotate(text, by):
    s_from = string.ascii_lowercase
    s_to = string.ascii_lowercase[by:] + string.ascii_lowercase[:by]
    cypher_table = string.maketrans(s_from, s_to)
    return text.translate(cypher_table)

user_input = raw_input('type a string: ').lower()
print rotate(user_input, 2)
type a string: abcxyz
cdezab
abcdefghijklmnopqrstuvwxyz
cdefghijklmnopqrstuvwxyzab

maketrans
创建一个映射表,将
s\u中的字符从
映射到
s\u到
translate
然后将此映射应用于字符串。

对于
z
应该发生什么?是否应该变成
b

您可以使用Python的和函数来执行以下操作:

import string

def rotate(text, by):
    s_from = string.ascii_lowercase
    s_to = string.ascii_lowercase[by:] + string.ascii_lowercase[:by]
    cypher_table = string.maketrans(s_from, s_to)
    return text.translate(cypher_table)

user_input = raw_input('type a string: ').lower()
print rotate(user_input, 2)
type a string: abcxyz
cdezab
abcdefghijklmnopqrstuvwxyz
cdefghijklmnopqrstuvwxyzab
这对整个字符串起作用,如下所示:

import string

def rotate(text, by):
    s_from = string.ascii_lowercase
    s_to = string.ascii_lowercase[by:] + string.ascii_lowercase[:by]
    cypher_table = string.maketrans(s_from, s_to)
    return text.translate(cypher_table)

user_input = raw_input('type a string: ').lower()
print rotate(user_input, 2)
type a string: abcxyz
cdezab
abcdefghijklmnopqrstuvwxyz
cdefghijklmnopqrstuvwxyzab
它是如何工作的?

如果从
打印
s_,并将
s_打印到
中,它们看起来如下所示:

import string

def rotate(text, by):
    s_from = string.ascii_lowercase
    s_to = string.ascii_lowercase[by:] + string.ascii_lowercase[:by]
    cypher_table = string.maketrans(s_from, s_to)
    return text.translate(cypher_table)

user_input = raw_input('type a string: ').lower()
print rotate(user_input, 2)
type a string: abcxyz
cdezab
abcdefghijklmnopqrstuvwxyz
cdefghijklmnopqrstuvwxyzab

maketrans
创建一个映射表,将
s\u中的字符从
映射到
s\u到
translate
然后将此映射应用于字符串。

对于
z
应该发生什么?是否应该变成
b

您可以使用Python的和函数来执行以下操作:

import string

def rotate(text, by):
    s_from = string.ascii_lowercase
    s_to = string.ascii_lowercase[by:] + string.ascii_lowercase[:by]
    cypher_table = string.maketrans(s_from, s_to)
    return text.translate(cypher_table)

user_input = raw_input('type a string: ').lower()
print rotate(user_input, 2)
type a string: abcxyz
cdezab
abcdefghijklmnopqrstuvwxyz
cdefghijklmnopqrstuvwxyzab
这对整个字符串起作用,如下所示:

import string

def rotate(text, by):
    s_from = string.ascii_lowercase
    s_to = string.ascii_lowercase[by:] + string.ascii_lowercase[:by]
    cypher_table = string.maketrans(s_from, s_to)
    return text.translate(cypher_table)

user_input = raw_input('type a string: ').lower()
print rotate(user_input, 2)
type a string: abcxyz
cdezab
abcdefghijklmnopqrstuvwxyz
cdefghijklmnopqrstuvwxyzab
它是如何工作的?

如果从
打印
s_,并将
s_打印到
中,它们看起来如下所示:

import string

def rotate(text, by):
    s_from = string.ascii_lowercase
    s_to = string.ascii_lowercase[by:] + string.ascii_lowercase[:by]
    cypher_table = string.maketrans(s_from, s_to)
    return text.translate(cypher_table)

user_input = raw_input('type a string: ').lower()
print rotate(user_input, 2)
type a string: abcxyz
cdezab
abcdefghijklmnopqrstuvwxyz
cdefghijklmnopqrstuvwxyzab


maketrans
创建一个映射表,将
s\u中的字符从
映射到
s\u到
<代码>翻译
然后将此映射应用于字符串。

显示您的尝试…@AvinashRaj道歉。“现在已经做了可能重复的展示你的尝试…”AvinashRaj道歉。“现在已经做了可能重复的展示你的尝试…”AvinashRaj道歉。“现在已经做了可能重复的展示你的尝试…”AvinashRaj道歉。现在已经做了可以复制的好了。您知道如何对整个字符串执行此操作吗?您可以为循环添加一个
。现在我修改了解决方案。这很好。你如何避开空格、标点符号以及字母表末尾的“y”和“z”?如果
if
do
ord\u letter=((ord(letter)+offset-97)%26)+97
,你可以避免
。正确,@Ale!之前我尝试使用
mod
查看是否需要添加
97
ord
'a'
)。。。在我没有试过之后。非常感谢。好的,这很好。你知道整根绳子怎么做吗?你可以