【问题标题】:Issue trying to return key and values inside a nested dictionary尝试在嵌套字典中返回键和值的问题
【发布时间】:2021-11-04 08:13:51
【问题描述】:

第一次在这里发布海报。我正在尝试为我正在从事的项目创建一个简单的 Python 转换程序,作为我注册的课程的一部分。

我正在尝试根据起始单位返回多个长度单位的转换。为此,我正在尝试使用嵌套字典。它必须通过 CLI 终端,所以我不能使用 ttinker 或类似的东西来提供 GUI。

我正在尝试以以下格式返回它:

x 厘米换算后等于:

x 公里
x 米
x英里等

相反,我使用当前版本的代码得到了这个回报(虽然我确实希望它会像这样返回,但这是最接近我想要的结果的结果):

x 厘米换算后等于:
x
x
x
公里
米
里程

我目前的代码如下:

            len_selection = pyip.inputMenu(["Nanometre", "Micrometre", "Millimetre", "Centimetre", "Metre", "Kilometre", "Inch", "Foot", "Yard", "Mile", "Nautical Mile"], numbered=True)
            print("\nEnter the number of your required unit\n")
            len_value = pyip.inputNum("\nPlease enter the length to convert:")

            length_conversion_factors = {
                'kilometre': {'metre': 0.001, 'centimetre': 0.00001, 'millimetre': 0.000001,
                'micrometre': 0.000000001, 'nanometre': 0.000000000001, 'mile': 1.60934,
                'yard': 0.000914397727272727, 'foot': 0.000304799242424242, 'inch': 2.53999368686869,
                'nautical mile': 1.852},

                'metre': {'kilometre': 1000, 'centimetre': 0.01, 'millimetre': 0.001, 'micrometre': 0.000001,
                'nanometre': 0.000000001, 'mile': 1609.34, 'yard': 0.914397727272727, 'foot': 0.304799242424242,
                'inch': 0.0253999368686869, 'nautical mile': 1852},

                'centimetre': {'kilometre': 100000, 'metre': 100, 'millimetre': 0.1,
                'micrometre': 0.0001, 'nanometre': 0.0000001, 'mile': 160934,
                'yard': 91.4397727272727, 'foot': 30.4799242424242, 'inch': 2.53999368686869,
                'nautical mile': 185200}}

            length_key = len_selection.lower()

            print(f"\n{len_value} {len_selection}s converted equals:")

            if length_key in length_conversion_factors:
                for value in length_conversion_factors[length_key]:
                    conv_value = len_value * float(length_conversion_factors[length_key][value])
                    print(conv_value)
                for key in length_conversion_factors[length_key]:
                    print(key)

我已经为此花了一天半的时间,现在我正在拔头发。

我已经尝试过'for key, value in length_conversion_factors[length_key]:'之类的方法,所以它都在一个 for 循环中运行,我收到一个 Value 错误,说明太多无法解包。我还尝试先解包密钥,将它们存储在变量中,然后使用 print 语句,但也没有任何乐趣。

我尝试使用“return key”和“return conv_value”,然后对它们使用单​​独的打印语句,但似乎根本无法正常工作。

我知道我可能遗漏了一些非常简单的东西。非常感谢提前

【问题讨论】:

    标签: python dictionary nested nested-loops


    【解决方案1】:

    Python 允许您在语句中打印多个内容:

    if length_key in length_conversion_factors:
        for value in length_conversion_factors[length_key]:
            conv_value = len_value * float(length_conversion_factors[length_key][value])
            print(conv_value, value)
    

    这给了我这样的输出:

    1 kilometres converted equals:
    0.001 metre
    1e-05 centimetre
    1e-06 millimetre
    1e-09 micrometre
    

    这似乎是您想要的。您甚至可以用 f 字符串替换最终的 print 语句,就像代码中已经出现的那样:print(f"{conv_value} {value}s")

    【讨论】:

    • 非常感谢....毕竟我不敢相信它这么简单。在之前的尝试中,我曾尝试在 print 语句中使用 key,但从未考虑过使用 value。经过无数小时的咬牙切齿,你拯救了我的牙齿!
    猜你喜欢
    • 2017-12-14
    • 1970-01-01
    • 1970-01-01
    • 2016-05-19
    • 2022-12-18
    • 2019-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多