代码之家  ›  专栏  ›  技术社区  ›  Fĭř ÛÅ

字典、列表和字符串

  •  0
  • Fĭř ÛÅ  · 技术社区  · 2 年前

    词典 ,它以 并将其拆分为单个 所以 词典

    我知道有一种方法可以简化代码并使其大大缩小,但我不知道如何实现。

    也许我应该用 在里面 变量 len()

    是的,我是编程新手。

    提前感谢!

    decoder = {
    "96": "0",
    "97": "1",
    "98": "2",
    "99": "3",
    "100": "4",
    "101": "5",
    "102": "6",
    "103": "7",
    "104": "8",
    "105": "9",
    }
    test = list(str(num) for num in 
    input("Enter the list items 
    separated by space: 
    ").strip().split()).
    length = len(test)
    if length == 1:
        a, = test
        print(decoder[a])
    elif length == 2:
        a, b = test
        print(decoder[a])
        print(decoder[b])
    elif length == 3:
        a, b, c = test
        print(decoder[a])
        print(decoder[b])
        print(decoder[c])
    elif length == 4:
        a, b, c, d = test
        print(decoder[a])
        print(decoder[b])
        print(decoder[c])
        print(decoder[d])
    elif length == 5:
        a, b, c, d, e = test
        print(decoder[a])
        print(decoder[b])
        print(decoder[c])
        print(decoder[d])
        print(decoder[e])
    elif length == 6:
        a, b, c, d, e, f = test
        print(decoder[a])
        print(decoder[b])
        print(decoder[c])
        print(decoder[d])
        print(decoder[e])
        print(decoder[f])
    elif length == 7:
        a, b, c, d, e, f, g = test
        print(decoder[a])
        print(decoder[b])
        print(decoder[c])
        print(decoder[d])
        print(decoder[e])
        print(decoder[f])
        print(decoder[g])
    elif length == 8:
        a, b, c, d, e, f, g, h = test
        print(decoder[a])
        print(decoder[b])
        print(decoder[c])
        print(decoder[d])
        print(decoder[e])
        print(decoder[f])
        print(decoder[g])
        print(decoder[h])
    elif length == 9:
        a, b, c, d, e, f, g, h, i = 
        test
        print(decoder[a])
        print(decoder[b])
        print(decoder[c])
        print(decoder[d])
        print(decoder[e])
        print(decoder[f])
        print(decoder[g])
        print(decoder[h])
        print(decoder[i])
    elif length == 10:
        a, b, c, d, e, f, g, h, i, j = 
        test
        print(decoder[a])
        print(decoder[b])
        print(decoder[c])
        print(decoder[d])
        print(decoder[e])
        print(decoder[f])
        print(decoder[g])
        print(decoder[h])
        print(decoder[i])
        print(decoder[j])
    
    2 回复  |  直到 2 年前
        1
  •  0
  •   Ignatius Reilly    2 年前

    您可以在中使用,与使用输入项创建列表相同,并使用for循环迭代这些项:

    decoder = {
    "96": "0",
    "97": "1",
    "98": "2",
    "99": "3",
    "100": "4",
    "101": "5",
    "102": "6",
    "103": "7",
    "104": "8",
    "105": "9",
    }
    
    test = [str(num) for num in input("Enter the list items separated by space: ").strip().split()]
    
    for item in test:
        print(decoder[item])
    

    另外,请注意我是如何定义的 test 使用列表理解:)

        2
  •  0
  •   Seth Speaks    2 年前

    这将帮助您处理解码器中没有的密钥。

    decoder = {
    "96": "0",
    "97": "1",
    "98": "2",
    "99": "3",
    "100": "4",
    "101": "5",
    "102": "6",
    "103": "7",
    "104": "8",
    "105": "9",
    }
    test = list(str(num) for num in input("Enter the list items separated by space: ").strip().split())
    for num in test:
        try:
            print(decoder[num])
        except KeyError:
            print(f"Num {num} is not in the decoder")