gpt4 book ai didi

python - 修改嵌套字典

转载 作者:行者123 更新时间:2023-11-30 23:35:16 25 4
gpt4 key购买 nike

鉴于这两个命令:

empty = {'151': {'1': 'empty', '0': 'empty', '2': '2.30'}}
full = {'151': {'1': 3.4, '0': 3.6, '2': 2}}

首先,我想检查 empty.keys() == full.keys() 是否成立,我想用相应的值替换 empty 值来自 full 字典。它应该导致:

not_empty = {'151': {'1': '3.4', '0': '3.6', '2': '2.30'}}

到目前为止我的解决方案:我想我会使用正则表达式来识别具有值的所有键,但无论出于何种原因,到目前为止我的代码都会生成一个空字典{}.

import re 
find_empty = re.findall("'(\d)':\s'empty'", str(empty))[0]

if empty.keys() == full.keys():
k = empty.values()[0].keys()
v = empty.values()[0].values()
print {k:v for k,v in empty.values()[0].iteritems()\
if empty.values()[0][find_empty] != 'empty'}

我希望它可以输出 {'151': {'2': '2.30'}} 作为一个良好的起点。不管怎样,我想对于这个任务,存在比正则表达式更干净的解决方案,所以欢迎任何提示!

最佳答案

正则表达式不是适合这项工作的工具。我建议采用如下递归方法。

empty = {'151': {'1': 'empty', '0': 'empty', '2': '2.30'}}
full = {'151': {'1': 3.4, '0': 3.6, '2': 2}}

def repl(a, b):
clean = {}
for k, v in a.items():
# This is the case where we want to replace what we have in b if we have something. Just in case, use the dict.get method and provide a default.
if v == 'empty':
clean[k] = b.get(k, 'Not there')
# If the value is another dict, then call this function with the value, and put the return as the value for our current key
elif isinstance(v, dict):
v_clean = repl(v, b.get(k, {}))
clean[k] = v_clean
# The value isn't equal to 'empty', and it isn't another dict, so just keep the current value.
else:
clean[k] = v
# Finally, return the cleaned up dictionary.
return clean

print repl(empty, full)

输出

{'151': {'1': 3.4, '0': 3.6, '2': '2.30'}}

编辑我不确定这是否能解决您的所有情况,但无论如何它可能值得一看。

empty = {'151': {'1': 'empty', '0': 'empty', '2': '2.30', '8': ['empty', 'empty', 5, {"foo2": "bar2", "1": "empty"}]}}
full = {'151': {'1': 3.4, '0': 3.6, '2': 2, '8': ['foo', 'bar', 'baz', {"foo3": "bar3", "1": "2"}]}}

def repl(a, b):
if isinstance(a, dict) and isinstance(b, dict):
clean = {}
for k, v in a.items():
# This is the case where we want to replace what we have in b if we have something. Just in case, use the dict.get method and provide a default.
if v == 'empty':
clean[k] = b.get(k, 'Not there')
# If the value is another dict, then call this function with the value, and put the return as the value for our current key
elif isinstance(v, dict):
v_clean = repl(v, b.get(k, {}))
clean[k] = v_clean
# The value isn't equal to 'empty', and it isn't another dict, so just keep the current value.
elif isinstance(v, list):
v_clean = repl(v, b.get(k, []))
clean[k] = v_clean
else:
clean[k] = v
# Finally, return the cleaned up dictionary.
elif isinstance(a, list) and isinstance(b, list):
clean = []
for item_a, item_b in zip(a, b):
if item_a == 'empty':
clean.append(item_b)
elif isinstance(item_a, dict):
clean_a = repl(item_a, item_b)
clean.append(clean_a)
else:
clean.append(item_a)
return clean

print repl(empty, full)

输出

{'151': {'1': 3.4, '0': 3.6, '2': '2.30', '8': ['foo', 'bar', 5, {'1': '2', 'foo2': 'bar2'}]}}

关于python - 修改嵌套字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17481622/

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