json 创建多级词典

7vhp5slm  于 2023-05-19  发布在  其他
关注(0)|答案(2)|浏览(171)

当我创建一个多级字典(或JSON)时,我必须在每一级进行初始化,它会覆盖旧的数据
比如我想创造一个

dict1 = {}
for i in ["i","you"]:
    for j in ["have", "has", "had"]:
        for k in ["a","an"]:
            for m in ["apple", "pear"]:
                for n in ["cat","dog"]:
                    dict1[i] = {j:{k:{"fruit":m,"animal":n}}} 
print(dict1)

我想知道

{'i': {'have': {'a': {'fruit': 'apple', 'animal': 'cat'}}},
 'you': {'have': {'a': {'fruit': 'apple', 'animal': 'dog'}}}

等等。但是我只收到最后2个排列,因为它们总是被覆盖。

djp7away

djp7away1#

一次性创建嵌套结构需要在每个级别上进行良好的初始化。在您的方法中,可以采用defaultdict

from collections import defaultdict

tree = lambda: defaultdict(tree)
dict1 = tree()

for i in ["i","you"]:
    for j in ["have", "has", "had"]:
        for k in ["a","an"]:
            for m in ["apple", "pear"]:
                for n in ["cat","dog"]:
                    dict1[i][j][k] = {"fruit":m,"animal":n}

输出不完全是一个dict,但以相同的方式工作。

nwlls2ji

nwlls2ji2#

每次迭代内部循环时都要重写键。使用list代替

lst = []
for i in ["i", "you"]:
    for j in ["have", "has", "had"]:
        for k in ["a", "an"]:
            for m in ["apple", "pear"]:
                for n in ["cat", "dog"]:
                    lst.append({i: {j: {k: {"fruit": m, "animal": n}}}})

print(lst)

输出

[{'i': {'have': {'a': {'fruit': 'apple', 'animal': 'cat'}}}},
 {'i': {'have': {'a': {'fruit': 'apple', 'animal': 'dog'}}}},
 {'i': {'have': {'a': {'fruit': 'pear', 'animal': 'cat'}}}},
 ...
 {'you': {'had': {'an': {'fruit': 'apple', 'animal': 'dog'}}}},
 {'you': {'had': {'an': {'fruit': 'pear', 'animal': 'cat'}}}},
 {'you': {'had': {'an': {'fruit': 'pear', 'animal': 'dog'}}}}]

编辑
如果添加了额外的唯一密钥,则可以保留dict

dict1 = {}
id = 0
for i in ["i", "you"]:
    for j in ["have", "has", "had"]:
        for k in ["a", "an"]:
            for m in ["apple", "pear"]:
                for n in ["cat", "dog"]:
                    dict1[id] = {i: {j: {k: {"fruit": m, "animal": n}}}}
                    id += 1

输出

{0: {'i': {'have': {'a': {'fruit': 'apple', 'animal': 'cat'}}}},
 1: {'i': {'have': {'a': {'fruit': 'apple', 'animal': 'dog'}}}},
 2: {'i': {'have': {'a': {'fruit': 'pear', 'animal': 'cat'}}}},
...
 45: {'you': {'had': {'an': {'fruit': 'apple', 'animal': 'dog'}}}},
 46: {'you': {'had': {'an': {'fruit': 'pear', 'animal': 'cat'}}}},
 47: {'you': {'had': {'an': {'fruit': 'pear', 'animal': 'dog'}}}}}

编辑二:
dict中的密钥必须是唯一的,您不能拥有i密钥多次。如果你不想使用上面的选项,唯一的方法是使用tuple作为密钥#

dict1 = {}
for i in ["i", "you"]:
    for j in ["have", "has", "had"]:
        for k in ["a", "an"]:
            dict1[(i, j, k)] = {"fruit": "apple", "animal": "dog"}
print(dict1[('i', 'have', 'a')]) # {'fruit': 'apple', 'animal': 'dog'}
print(dict1[('i', 'have', 'an')]) # {'fruit': 'apple', 'animal': 'dog'}

fruitanimal的内部循环中仍然会有重复键的问题(如果您确实需要它们),要获得所有选项,您仍然需要一个列表

dict1 = {}
for i in ["i", "you"]:
    for j in ["have", "has", "had"]:
        for k in ["a", "an"]:
            dict1[(i, j, k)] = []
            for m in ["apple", "pear"]:
                for n in ["cat", "dog"]:
                    dict1[(i, j, k)].append({"fruit": m, "animal": n})

print(dict1[('i', 'have', 'a')])
print(dict1[('i', 'have', 'an')])

# [{'fruit': 'apple', 'animal': 'cat'}, {'fruit': 'apple', 'animal': 'dog'}, {'fruit': 'pear', 'animal': 'cat'}, {'fruit': 'pear', 'animal': 'dog'}]
# [{'fruit': 'apple', 'animal': 'cat'}, {'fruit': 'apple', 'animal': 'dog'}, {'fruit': 'pear', 'animal': 'cat'}, {'fruit': 'pear', 'animal': 'dog'}]

相关问题