gpt4 book ai didi

python - 如何将一些数据添加到 python 字典中的特定 'path'?

转载 作者:太空狗 更新时间:2023-10-30 02:15:33 26 4
gpt4 key购买 nike

比如我要定义一个函数:

def add_value(dic, path, value):
# dict is a Dictionary to be modified
# path is a list, like ['A', 'B', 'C', ...]
# value is a list or tuple
...
...
return dic


d = {}
d = add_value(d, ['A', 'B'], ('log.txt', '12KB'))
print(d)
d = add_value(d, ['X', 'Y', 'Z'], ('backup.bin', '17MB'))
print(d)
d = add_value(d, ['X', 'Y', 'Z'], ('file.lst', '5KB'))
print(d)

接下来的输出应该是这样的:

{'A': {'B': {'log.txt': '12KB'}}}

{'A': {'B': {'log.txt': '12KB'}}, 'X': {'Y': {'Z': {'backup.bin': '17MB'}}}}

{'A': {'B': {'log.txt':'12KB'}}, 'X': {'Y': {'Z': {'backup.bin': '17MB', 'file.lst': '5KB'}}}}

我试过这样的:

import json
def add_value(dic, path, value):
# dic is a dict to be modified
# path is a list
# value is a tumple like ('file1.txt': '20kb')
length = len(path)
if length == 1:
if path[0] not in dic:
dic[path[0]] = {}
else:
dic[path[0]].append(value)
return dic
elif length == 2:
if path[0] not in dic:
dic[path[0]] = {}
if path[1] not in dic[path[0]]:
dic[path[0]][path[1]] = {}
dic[path[0]][path[1]][value[0]] = value[1]
return dic
elif length == 3:
if path[0] not in dic:
dic[path[0]] = {}
if path[1] not in dic[path[0]]:
dic[path[0]][path[1]] = {}
if path[2] not in dic[path[0]][path[1]]:
dic[path[0]][path[1]][path[2]] = {}
dic[path[0]][path[1]][path[2]][value[0]] = value[1]
return dic
elif length == 4:
if path[0] not in dic:
dic[path[0]] = {}
if path[1] not in dic[path[0]]:
dic[path[0]][path[1]] = {}
if path[2] not in dic[path[0]][path[1]]:
dic[path[0]][path[1]][path[2]] = {}
if path[3] not in dic[path[0]][path[1]][path[2]]:
dic[path[0]][path[1]][path[2]][path[3]] = {}
dic[path[0]][path[1]][path[2]][path[3]][value[0]] = value[1]
return dic
else:
return



d = {}
d = add_value(d, ['A', 'B'], ('log.txt', '12KB'))
d = add_value(d, ['A', 'B', 'C',], ('log2.txt', '34kb'))
d = add_value(d, ['A', 'G'], ('log2.txt', '2kb'))
d = add_value(d, ['X', 'Y', 'Z'], ('log2.txt', '3kb'))
print(json.dumps(d, indent=2))

输出是:

{   "X": {
"Y": {
"Z": {
"log2.txt": "3kb"
}
} }, "A": {
"G": {
"log2.txt": "2kb"
},
"B": {
"C": {
"log2.txt": "34kb"
},
"log.txt": "12KB"
} } }

看起来很蠢。如果我想 add_value(d, ['A', 'B', 'C', ... , 'Y', 'Z'], ('1.jpg', '1.2MB')) ,我需要编写大量代码。

最佳答案

你可以使用这个:

def add_value(d, path, value):
curr = d
for key in path:
if key not in curr:
curr[key] = {}
curr = curr[key]
k, v = value
curr[k] = v

return d

d = {}
d = add_value(d, ['A', 'B'], ('log.txt', '12KB'))
print(d)
d = add_value(d, ['X', 'Y', 'Z'], ('backup.bin', '17MB'))
print(d)
d = add_value(d, ['X', 'Y', 'Z'], ('file.lst', '5KB'))
print(d)

输出:

{'A': {'B': {'log.txt': '12KB'}}}
{'A': {'B': {'log.txt': '12KB'}}, 'X': {'Y': {'Z': {'backup.bin': '17MB'}}}}
{'A': {'B': {'log.txt': '12KB'}}, 'X': {'Y': {'Z': {'backup.bin': '17MB', 'file.lst': '5KB'}}}}

该函数将检查当前键是否已存在于字典中。如果它不存在,它将创建一个新的键,并将一个空字典作为值。

关于python - 如何将一些数据添加到 python 字典中的特定 'path'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49622962/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com