Python未打印完整的数据结构

2024-04-29 11:09:42 发布

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

因此,我有一个Python文件,其中包含大量嵌套字典和列表的数据,如下所示:

recipes = {
    "recipe": {
        "name": "solar_panel",
        "type": "craft",
        "ingredients": {
            "input": [
                "coal_dust",
                "glass",
                "coal_dust",
                "glass",
                "coal_dust",
                "glass",
                "electronic_circuit",
                "generator",
                "electronic_circuit"
            ],
            "output": {
                "item": "solar_panel",
                "quantity": 1
            }
        }
    },
    "recipe": {
        "name": "re_battery",
        "type": "craft",
        "ingredients": {
            "input": [
                "nothing",
                "insulated_copper_cable",
                "nothing",
                "tin",
                "redstone",
                "tin",
                "tin",
                "redstone",
                "tin"
            ],
            "output": {
                "item": "re_battery",
                "quantity": 1
            }
        }
    }
}

我有一个Python脚本

import vanilla
print(vanilla.recipes)

人们可能会认为is只打印完整的数据结构,但实际上它只打印最后一个子项(列表中的第23-43行)。我觉得我错过了一些明显的东西。你知道吗


Tags: name列表inputtyperecipeelectronictinpanel
2条回答

因为键是相同的-recipe,所以它只保存字典中的最后一项。你知道吗

>>> {'a': 1, 'a': 2}
{'a': 2}

只有一个子项存在,因为每个键必须是唯一的。如果您想要一个键有多个值,那么您必须将它们放入一个序列中,例如列表或元组。你知道吗

相关问题 更多 >