我一直在尝试将python中列表中的字母更改为类中相应的字母数字。我已经尝试了所有我知道的方法,但我仍然无法做到正确。如果有人可以回答这个问题,那将对我有很大的帮助,谢谢
def main():
miriam = True
def letters():
a = 1
b = 2
c = 3
d = 4
e = 5
f = 6
g = 7
h = 8
i = 9
j = 10
k = 11
l = 12
m = 13
n = 14
o = 15
p = 16
q = 17
r = 18
s = 19
t = 20
u = 21
v = 22
w = 23
x = 24
y = 25
z = 26
A = 1
B = 2
C = 3
D = 4
E = 5
F = 6
G = 7
H = 8
I = 9
J = 10
K = 11
L = 12
M = 13
N = 14
O = 15
P = 16
Q = 17
R = 18
S = 19
T = 20
U = 21
V = 22
W = 23
X = 24
Y = 25
Z = 26
while miriam == True:
codeDeCode = input("would you like to code or decode? ")
if codeDeCode == "code":
code = input("enter the text you would like to code")
listedCode = list(code)
listedCodeAmount = len(listedCode)
print(listedCodeAmount)
for x in range(0, listedCodeAmount):
listedCode[x] = letters()
print(listedCode)
我得出的结果是
>>> main()
would you like to code or decode? code
enter the text you would like to codemiriam
6
[None, None, None, None, None, None]
would you like to code or decode?
我想得到像这样的结果
[13, 9, 18, 9, 1, 13]
我真的很感谢有人对此的帮助,谢谢。
答案 0 :(得分:2)
In [29]: mydict=dict(zip(string.letters,[ord(c)%32 for c in string.letters]))
In [30]: code="miriam"
In [31]: li=list(code)
In [32]: [mydict[i] for i in li]
Out[32]: [13, 9, 18, 9, 1, 13]
答案 1 :(得分:1)
这有效:
import string
构建从字母到数字的字典映射:
codes_letter_to_number = {letter: i for i, letter in
enumerate(string.ascii_lowercase, 1)}
和另一种方式:
codes_number_to_letter = {v: k for k, v in codes_letter_to_number.items()}
代码:
[codes_letter_to_number[letter.lower()] for letter in 'miriam']
给出:
[13, 9, 18, 9, 1, 13]
解码:
''.join(codes_number_to_letter[i] for i in [13, 9, 18, 9, 1, 13])
给出:
'miriam'
现在,把所有功能放在一个很好的功能中:
import string
LETTER_TO_NUMBER = {letter: i for i, letter in enumerate(string.ascii_lowercase, 1)}
NUMBER_TO_LETTER = {v: k for k, v in LETTER_TO_NUMBER.items()}
def code(word):
return [LETTER_TO_NUMBER[letter.lower()] for letter in word]
def decode(number_list):
return ''.join(NUMBER_TO_LETTER[i] for i in number_list)
现在:
code('miriam')
返回:
[13, 9, 18, 9, 1, 13]
和
decode([13, 9, 18, 9, 1, 13])
给出:
'miriam'
答案 2 :(得分:1)
你可以使用字典:
My_Dictionary = {'a': 1, 'b': 2} # you can fill in the rest
My_Message = "abba"
My_Numbers = []
for letter in My_Message:
My_Numbers.append(My_Dictionary[letter])
print My_Numbers
答案 3 :(得分:1)
for x in range(0, listedCodeAmount):
listedCode[x] = letters()
这不是函数的工作方式。您的函数没有返回值,因此返回None
。您试图将该功能视为列表或其他容器。您只是在函数范围中设置了一堆变量,然后在完成后将它们放在地板上。
import string
miriam = True
def get_letter_dict():
return {val: idx for idx, val in enumerate(string.ascii_lowercase, 1)}
while miriam == True:
letter_dict = letters()
code_decode = input("would you like to code or decode? ")
if code_decode.lower() == "code":
code = input("enter the text you would like to code:\n") # makes it easier to read
listed_code= [letters[c] for c in code.lower()]
print(listed_code)
所以我们做的第一件事就是导入string
模块,这将有助于我们制作字典。然后我们将您的变量设置为True
,以便稍后退出该程序。
接下来,我们制作了一个函数来获取我们的字典。你当然可以手工做到这一点,但这更容易。我使用了dictionary comprehension。请注意我在函数中如何使用return
(了解有关它here的更多信息)。这使得我可以将函数的结果保存到变量中。注意我使用string.ascii_lowercase
来构建字典。这是一个包含a-z的所有小写字母的字符串。使用键或比较字符串时,大小写很重要。记住'm' != 'M'
。您可以阅读枚举here
下面的一些代码应该看起来很熟悉,所以我不会进入任何细节。但是我确实改变了你建立列表的方式。我使用了列表理解,你可以阅读here。
我还将变量名称更改为建议的Python命名约定。变量在其他语言中命名为likeThis
,在Python中我们将变量命名为like_this
。它更容易阅读。
答案 4 :(得分:0)
>>> [ord(c)-64 for c in 'MiRiaM'.upper()]
[13, 9, 18, 9, 1, 13]
如果你想知道64 ...那是因为ord('A')
是65.如果你想更清楚,你可以这样做:
>>> [ord(c)-ord('A')+1 for c in 'MiRiaM'.upper()]
[13, 9, 18, 9, 1, 13]