【发布时间】:2021-04-17 06:44:13
【问题描述】:
我正在制作配置程序来帮助用户配置.json 文件。该程序的功能之一是检查保存的json 是否与用户创建的新json 文件相同。如果两个.json不相同,它会告诉用户保存程序中正在配置的.json文件。
我的第一个想法是每次检查两个.json 文件是否相同时都从.json 文件中读取。它看起来像这样:
# read from the saved json file
new_settings = {"key1": 1, "key2": 2, "array1": []} # json.load(open('config.json', 'r').read())
# modifying new_settings
new_settings['array1'].append('Data')
def checkIsDifferent():
# read from the saved json file
saved_settings = {"key1": 1, "key2": 2, "array1": []} # json.load(open('config.json', 'r').read())
if saved_settings == new_settings:
print('Configuration is saved')
else:
print('(*)Configuration is not saved')
我不认为经常从文件中读取将是比较“设置”的好方法,所以我想出了另一种方法,将保存的 .json 复制到一个变量中,然后使用要比较的变量:
saved_settings = {"key1": 1, "key2": 2, "array1": []} # read from the saved json file
new_settings = saved_settings.copy()
# modify
new_settings['array1'].append('Data')
def checkIsDifferent():
if saved_settings == new_settings:
print('Configuration is saved')
else:
print('(*)Configuration is not saved')
第一个解决方案符合预期。运行checkIsDifferent()函数时输出“(*)配置未保存”。但是当我在第二个解决方案上运行checkIsDifferent() 时,它会输出“配置已保存”。
python 中的dict.copy() 坏了吗?对于第二种解决方案,我该如何解决?
系统环境:
Python 版本:Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)]
操作系统:Windows 10
【问题讨论】:
-
dict.copy()执行“浅”复制:saved_settings['array1']和new_settings['array1']都是对同一个列表的引用,并且附加会影响两者。请改用copy.deepcopy -
很确定你需要做一个深拷贝才能让它们与众不同。指向列表的指针将被复制,但列表将是相同的。
-
当您调用 copy 时,它将采用“key1”并为 1 分配新内存,因为 1 是不可变的(它是一个整数)。但是当它使用“array1”时,它不会为 [] 分配新内存,因为列表是可变的。
-
@LPR Python 还必须为空列表分配内存。
-
@mkrieger1 是的,您是对的,但是当您以这种方式执行复制时,两个字典中的列表将是相同的。它不会创建新列表。
标签: python json dictionary