【发布时间】:2019-05-28 03:33:38
【问题描述】:
我遇到了一个奇怪的行为——变量没有明显的原因改变了它的值。请帮助我了解发生这种情况的原因/原因以及如何避免这种情况。详细信息在下面作为内联 cmets 给出。为方便起见,将打印语句添加到代码中。
这个想法是通过将高阶数据相乘来展平 JSON 文件,如下所示。
非常感谢提前。
预期的输出是:
[{'records': '563'},
{'records': '563', 'id': '1111111', 'title': 'alignable', 'status': 'Completed'},
{'records': '563', 'id': '2222222', 'title': ' no links', 'status': 'something'}]
我的代码
test_json = {"records": "563",
"campaign": [{"id": "1111111", "title": "alignable", "status": "Completed"},
{"id": "2222222", "title": " no links", "status": "something"}]
}
def data_multiplication(initial_nested_data):
out = [{}]
def data_multiplication_(nested_data):
if isinstance(nested_data, list) and len(nested_data) > 0:
base_dic = out[-1] # with the current example, this line should ( and is ) executed once only.
# The main question is how it happens that base_dic changes from [{'records': '563'}]
for x in nested_data:
print(f'The base_dictionary before the desired append is: {base_dic}')
# Works like a breeze when I append the out list with a static dic as below.
out.append({1: 'This here should be the basic dictionary'}) # Comment this line to switch
# Starts behaving abnormally if I append the out list as below.
# out.append(base_dic) # Uncomment this line to switch
print(f'The base_dictionary after the desired append is: {base_dic}')
data_multiplication_(x)
elif isinstance(nested_data, dict) or len(nested_data) == 0:
for x in nested_data:
if (isinstance(nested_data[x], list) or isinstance(nested_data[x], dict)) and len(nested_data[x]) > 0:
data_multiplication_(nested_data[x])
else:
out[-1][x] = nested_data[x]
data_multiplication_(initial_nested_data)
return out
if __name__ == '__main__':
result = data_multiplication(test_json)
【问题讨论】:
-
程序的实际输出是什么?什么变量在变化?
-
append不会复制。 Python 不会像在 C++ 中看到的那样隐式复制对象。 -
请发送minimal reproducible example。 (最小的部分)
标签: python json python-3.x recursion