简体   繁体   English

如何用逗号分隔列表输入?

[英]How to separate list input by comma?

So I'm making a A1Z26 cipher decoder where I could enter the numbers in and it'll return a corresponding letter, eg 8,5,12,12,0 --> h,e,l,l,o.所以我正在制作一个 A1Z26 密码解码器,我可以在其中输入数字,它会返回一个相应的字母,例如 8,5,12,12,0 --> h,e,l,l,o。

However, I'm having trouble figuring out how to make python take each number as input as opposed to splitting them into digits.但是,我无法弄清楚如何让 python 将每个数字作为输入,而不是将它们拆分为数字。

Any help would be appreciated.任何帮助,将不胜感激。 Thanks谢谢

EDIT: Here's the code I've written so far:`编辑:这是我到目前为止编写的代码:`

dic = {dictionary that translates numbers to letters}
mid = []
output = []

input = raw_input("Enter the code here: ")
splitinput = list(input)

for i in splitinput:
    if i != ",":
        mid.append(i)

mid = [int(i) for i in buffer]

for i in mid:
    output.append(dic[i])


print output

So for it to stop splitting each number into digits I would need to use something other than the list function.因此,为了停止将每个数字拆分为数字,我需要使用列表功能以外的其他功能。

Something like this:像这样的东西:

myint = 123456789
mystr = str(myint)
print(','.join(mystr[::2]))

Alright, I found it.好的,我找到了。 This is the code that gives me the desired output if anyone will be interested:如果有人感兴趣,这是给我所需输出的代码:

inv_alphabet = {contains the mapping of each number to letter}
output = []

code_input = str(raw_input("Enter your cipher here: "))
split_code = code_input.split(",")

split_code = [int(i) for i in split_code]

for i in split_code:
    output.append(inv_alphabet[i])

print output

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM