为什么我不能将多个字典值放入同一个变量中

2024-10-02 00:23:00 发布

您现在位置:Python中文网/ 问答频道 /正文

def ULIfrom(in1):
    out1 = 0
    out2 = ""
    out3 = ""
    dict2 = {' ':int(0),'a':int(1),'b':int(2),'c':int(3),'d':int(4),'e':int(5),'f':int(6),'g':int(7),'h':int(8),
             'i':int(9),'j':int(10),'k':int(11),'l':int(12),'m':int(13),'n':int(14),'o':int(15),'p':int(16),'q':int(17),
             'r':int(18),'s':int(19),'t':int(20),'u':int(21),'v':int(22),'w':int(23),'x':int(24),'y':int(25),'z':int(26)
             }
    for x in in1:
        if x == ".":
            out2 = dict2.get(out1)
            out3 += out2
        else:
            if x == "U":
                out1 += 10
            elif x == "L":
                out1 += 5
            elif x == "I":
                out1 += 1
    return(out2)

print (ULIfrom(input("enter your encoded phrase:")))

我对编码相当陌生,所以当我尝试在out3变量中存储多个字典值时,我不知道发生了什么,它说您不能组合非类型和字符串,因为输出的字典值应该是字符串


Tags: 字符串inforgetif字典defint
3条回答

您生成了一个从单个字符串键映射到整数值的字典,但代码的其余部分似乎需要相反的映射—从整数转换为字符

此外,当您将out2连接到out3时,您不会测试它是从字典查找返回的字符串(如果找到键)还是None值。如果out2 == None,则应跳过连接,或者为get调用提供默认字符串值(如果键不在字典中,则返回):

        out2 = dict2.get(out1, '')

不知何故,你的功能结果应该是什么还不清楚。我猜您希望遍历in1中的所有字符,并将字典中的值作为总数相加。您可能希望在将来为变量使用更有意义的名称,以便您和其他读者能够了解它们应该包含哪些内容

不需要写下映射字典的每个值。写一本字典,让Python帮你完成。字母'U''L''I'的大小写可以添加到字典中,因此您不必在代码后面处理它们

然后在字符串上循环,从字典中找到条目(默认值为0),并添加这些值以获得总数

import string

def ULIfrom(data):
    mapping = {char: ord(char) - ord('a') + 1 for char in string.ascii_lowercase}
    mapping['U'] = 10
    mapping['L'] = 5
    mapping['I'] = 1

    total = 0
    for character in data:
        total += mapping.get(character, 0)

    return total


print(ULIfrom(input("enter your encoded phrase:")))

如果替换,则可以进一步缩短此代码

    total = 0
    for character in data:
        total += mapping.get(character, 0)

    return total

    return sum(mapping.get(character, 0) for character in data)

澄清问题后,我将代码调整为:

def get_text_from_ULI(uli):
    MAP_CODE = {'U': 10, 'L': 5, 'I': 1}
    MAP_NUMBER_TO_CHAR = {ord(char) - ord('a') + 1: char for char in string.ascii_lowercase}

    text = ''
    words = uli.split('/')
    for word in words:
        codes = word.split('.')
        for code in codes:
            number = sum(MAP_CODE.get(c, 0) for c in code)
            character = MAP_NUMBER_TO_CHAR.get(number, '')
            text += character
        text += ' '
    return text

print(get_text_from_ULI('LIII.L.UII.UII.UL./.UUIII.UL.ULIII.IIII.'))

结果是hello word


现在来看看真正邪恶的东西。我假设输入是有效的,这样代码就短了一点。这是一条单行线:

def get_text_from_ULI(uli):
    return ' '.join(''.join({ord(char)-ord('a')+1:char for char in string.ascii_lowercase}[sum({'U':10,'L':5,'I':1}[c]for c in code)]for code in word.split('.') if code)for word in uli.split('/'))

孩子们,不要在家里这样做


感谢大家的帮助,我终于完成了代码

def ULIto(in1):
dict1 = {'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
         }
out1 = ""
out2 = ""
for x in in1:
    if x == " ":
        out2 = (out2 + "/")
    else:
        out1 = 0
        out1 =(dict1.get(x))
        if out1 >= 10:
            while out1 >= 10:
                out1 = out1-10
                out2 = (out2 + "U")
        if out1 >= 5:
            while out1 >= 5:
                out1 = out1-5
                out2 = (out2 + "L")
        if out1 >= 1:
            while out1 >= 1:
                out1 = out1-1
                out2 = (out2 + "I")
        out2 = out2+"."
out2 = out2+" "

return out2

打印(ULIto(输入(“输入您的(仅限小写字母)短语:”))


这是将单词转换为代码的函数

def ULIfrom(in1):
out1 = 0
out2 = ""
dict2 = {0 : " ",1 : "a",2 : "b",3 : "c",4 : "d",5 : "e",6 : "f",
         7 : "g",8 : "h",9 : "i",10 : "j",11 : "k",12 : "l",13 : "m",
         14 : "n",15 : "o",16 : "p",17 : "q",18 : "r",19 : "s",20 : "t",
         21 : "u",22 : "v",23 : "w",24 : "x",25 : "y",26 : "z"
         }
for x in in1:
    if x == "/":
        out2 += " "
    if x == ".":
        out2 += dict2.get(out1, '')
        out1 = 0
    else:
        if x == "U":
            out1 += 10
        elif x == "L":
            out1 += 5
        elif x == "I":
            out1 += 1
return out2

打印(ULIfrom(输入(“输入您的编码短语:”))


此函数用于将代码改回文字

相关问题 更多 >

    热门问题