gio*_*lio 0 python pretty-print data-structures
所以我有一个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
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个Python脚本就是这个
import vanilla
print(vanilla.recipes)
Run Code Online (Sandbox Code Playgroud)
人们会认为这只是打印完整的数据结构,但事实上它只打印最后一个子项(列表中的第23-43行).我觉得我在这里遗漏了一些明显的东西.
这是因为键是相同的 - recipe
所以它只保存字典中的最后一项.
>>> {'a': 1, 'a': 2}
{'a': 2}
Run Code Online (Sandbox Code Playgroud)