我有以下嵌套字典:
ex_dict = {'path1':
{'$variable1': '2018-01-01',
'$variable2': '2020-01-01',
'$variable3': '$variable1',
'$variable4': '$variable3'},
'path2':
{'$variable1': '2018-01-01',
'$variable2': '2020-01-01',
'$variable3': '$variable1',
'$variable4': '$variable1 + $variable2'}
}
我想用另一个键的dict值替换为dict键指定的任何$ variableX,如果在原始dict键的值中找到另一个dict值 if 的键。请参见下面的示例输出:
{'path1':
{'$variable1': '2018-01-01',
'$variable2': '2020-01-01',
'$variable3': '2018-01-01', # Substituted with value from key:variable1
'$variable4': '2018-01-01'}, # Substituted with value from key:variable3 (after variable3 was substituted with variable1)
'path2':
{'$variable1': '2018-01-01',
'$variable2': '2020-01-01',
'$variable3': '2018-01-01', # Substituted with value from key:variable1
'$variable4': '2018-01-01 + 2020-01-01'} # Substituted with value from key:variable3 (after variable3 was substituted with variable1) and key:variable2
}
有没有人有任何建议?
答案 0 :(得分:2)
您可以通过递归遍历字典并使用re
库进行替换来替换
import re
def process_dict(d):
reprocess = []
keys = d.keys()
while keys:
for k in keys:
v = d[k]
if isinstance(v, dict):
process_dict(v)
elif '$' in v:
d[k] = re.sub(r'\$\w+', lambda m: d[m.group(0)] if m.group(0) in d else m.group(0), v)
if '$' in d[k] and d[k] != v:
reprocess.append(k)
keys = reprocess
reprocess = []
编辑:
我添加了一个重新处理步骤来处理引用被链接的情况,并且需要多次遍历字典中的某些键才能完全处理它们。
答案 1 :(得分:1)
不是非常 Pythonic 解决方案,但是,诀窍是:
array_merge
输出:
ex_dict = {'path1':
{'$variable1': '2018-01-01',
'$variable2': '2020-01-01',
'$variable3': '$variable1',
'$variable4': '$variable3'},
'path2':
{'$variable1': '2018-01-01',
'$variable2': '2020-01-01',
'$variable3': '$variable1',
'$variable4': '$variable1 + $variable2'}
}
for path, d in ex_dict.items():
for k, v in d.items():
if v.startswith('$variable'):
try:
if '+' in v:
ex_dict[path][k] = ' + '.join(ex_dict[path][x.strip()] for x in v.split('+'))
else:
ex_dict[path][k] = ex_dict[path][v]
except KeyError:
pass
pprint(ex_dict)
答案 2 :(得分:0)
您可以使用词典理解:
import re
dict = {'path1':
{'$variable1': '2018-01-01',
'$variable2': '2020-01-01',
'$variable3': '$variable1',
'$variable4': '$variable3'},
'path2':
{'$variable1': '2018-01-01',
'$variable2': '2020-01-01',
'$variable3': '$variable1',
'$variable4': '$variable1 + $variable2'}
}
final_data = {a:{c:d if re.findall('\d+-\d+-\d+', d) else \
re.sub('\$\w+', '{}', d).format(*[b[i] for i in re.findall('\$\w+', d)]) \
for c, d in b.items()} for a, b in dict.items()}
输出:
{'path2': {'$variable4': '2018-01-01 + 2020-01-01', '$variable2': '2020-01-01', '$variable3': '2018-01-01', '$variable1': '2018-01-01'}, 'path1': {'$variable4': '$variable1', '$variable2': '2020-01-01', '$variable3': '2018-01-01', '$variable1': '2018-01-01'}}