为什么在字典中更改值后,所有其他值也随之更改?
Why after change value in dictionary, all other value also changed too?
为什么改变字典中指定索引的一个值后,所有其他索引值也都改变了,为什么会这样?
with open(BotAccountFile, 'r') as account_file:
for num, line in enumerate(account_file, 1):
username = line.split(',')[0].strip()
BotAccountsLinksList[num] = {'UserName': username, 'Password': line.split(',')[1].strip(),
'CompletedDownload': False}
BotLinksList = {}
BotLinksList['LinkData'] = {}
with open(BotDownloadURIsFile, 'r') as link_file:
for item in BotAccountsLinksList:
for num, line in enumerate(link_file, 1):
BotLinksList['LinkData'][num] = {'Link': line.strip(), 'CompletedDownload': False}
BotAccountsLinksList[item].update(BotLinksList)
还有我的字典:
BotAccountsLinksList = {}
BotAccountsLinksList = {1: {'UserName': 'user1', 'Password': 'pass1',
'CompletedDownload': False, 'LinkData': {1: {'Link': 'www.yahoo.com',
'CompletedDownload': False}, 2: {'Link': 'www.google.com',
'CompletedDownload': False}}}, 2: {'UserName': 'user2', 'Password': 'pass2',
'CompletedDownload': False, 'LinkData': {1: {'Link': 'www.yahoo.com',
'CompletedDownload': False}, 2: {'Link': 'www.google.com',
'CompletedDownload': False}}}, 3: {'UserName': 'user3', 'Password': 'pass3',
'CompletedDownload': False, 'LinkData': {1: {'Link': 'www.yahoo.com',
'CompletedDownload': False}, 2: {'Link': 'www.google.com',
'CompletedDownload': False}}}, 3: {'UserName': 'user4', 'Password': 'pass4',
'CompletedDownload': False, 'LinkData': {1: {'Link': 'www.yahoo.com',
'CompletedDownload': False}, 2: {'Link': 'www.google.com',
'CompletedDownload': False}}}}
我使用此代码仅更改索引 1 中的一个值:
BotAccountsLinksList[1]['LinkData'][1]['CompletedDownload'] = True # {'Link': 'www.yahoo.com', 'CompletedDownload': True}
但是在运行这段代码之后,其他索引值也发生了变化:
BotAccountsLinksList = {1: {'UserName': 'user1', 'Password': 'pass1',
'CompletedDownload': False, 'LinkData': {1: {'Link': 'www.yahoo.com',
'CompletedDownload': True}, 2: {'Link': 'www.google.com',
'CompletedDownload': False}}}, 2: {'UserName': 'user2', 'Password': 'pass2',
'CompletedDownload': False, 'LinkData': {1: {'Link': 'www.yahoo.com',
'CompletedDownload': True}, 2: {'Link': 'www.google.com',
'CompletedDownload': False}}}, 3: {'UserName': 'user3', 'Password': 'pass3',
'CompletedDownload': False, 'LinkData': {1: {'Link': 'www.yahoo.com',
'CompletedDownload': True}, 2: {'Link': 'www.google.com',
'CompletedDownload': False}}}, 3: {'UserName': 'user4', 'Password': 'pass4',
'CompletedDownload': False, 'LinkData': {1: {'Link': 'www.yahoo.com',
'CompletedDownload': True}, 2: {'Link': 'www.google.com',
'CompletedDownload': False}}}}
我在 Windows 10 64 位上使用 Python 3.8。
BotAccountsLinksList[item].update(BotLinksList)
正在做
for k,v in BotLinksList.items():
BotAccountsLinksList[item][k] = v
BotLinksList
值将在多个 BotAccountsLinksList
键之间共享。不要多次重复使用相同的字典或进行深度复制
import copy
BotAccountsLinksList[item].update(copy.deepcopy(BotLinksList))
为什么改变字典中指定索引的一个值后,所有其他索引值也都改变了,为什么会这样?
with open(BotAccountFile, 'r') as account_file:
for num, line in enumerate(account_file, 1):
username = line.split(',')[0].strip()
BotAccountsLinksList[num] = {'UserName': username, 'Password': line.split(',')[1].strip(),
'CompletedDownload': False}
BotLinksList = {}
BotLinksList['LinkData'] = {}
with open(BotDownloadURIsFile, 'r') as link_file:
for item in BotAccountsLinksList:
for num, line in enumerate(link_file, 1):
BotLinksList['LinkData'][num] = {'Link': line.strip(), 'CompletedDownload': False}
BotAccountsLinksList[item].update(BotLinksList)
还有我的字典:
BotAccountsLinksList = {}
BotAccountsLinksList = {1: {'UserName': 'user1', 'Password': 'pass1',
'CompletedDownload': False, 'LinkData': {1: {'Link': 'www.yahoo.com',
'CompletedDownload': False}, 2: {'Link': 'www.google.com',
'CompletedDownload': False}}}, 2: {'UserName': 'user2', 'Password': 'pass2',
'CompletedDownload': False, 'LinkData': {1: {'Link': 'www.yahoo.com',
'CompletedDownload': False}, 2: {'Link': 'www.google.com',
'CompletedDownload': False}}}, 3: {'UserName': 'user3', 'Password': 'pass3',
'CompletedDownload': False, 'LinkData': {1: {'Link': 'www.yahoo.com',
'CompletedDownload': False}, 2: {'Link': 'www.google.com',
'CompletedDownload': False}}}, 3: {'UserName': 'user4', 'Password': 'pass4',
'CompletedDownload': False, 'LinkData': {1: {'Link': 'www.yahoo.com',
'CompletedDownload': False}, 2: {'Link': 'www.google.com',
'CompletedDownload': False}}}}
我使用此代码仅更改索引 1 中的一个值:
BotAccountsLinksList[1]['LinkData'][1]['CompletedDownload'] = True # {'Link': 'www.yahoo.com', 'CompletedDownload': True}
但是在运行这段代码之后,其他索引值也发生了变化:
BotAccountsLinksList = {1: {'UserName': 'user1', 'Password': 'pass1',
'CompletedDownload': False, 'LinkData': {1: {'Link': 'www.yahoo.com',
'CompletedDownload': True}, 2: {'Link': 'www.google.com',
'CompletedDownload': False}}}, 2: {'UserName': 'user2', 'Password': 'pass2',
'CompletedDownload': False, 'LinkData': {1: {'Link': 'www.yahoo.com',
'CompletedDownload': True}, 2: {'Link': 'www.google.com',
'CompletedDownload': False}}}, 3: {'UserName': 'user3', 'Password': 'pass3',
'CompletedDownload': False, 'LinkData': {1: {'Link': 'www.yahoo.com',
'CompletedDownload': True}, 2: {'Link': 'www.google.com',
'CompletedDownload': False}}}, 3: {'UserName': 'user4', 'Password': 'pass4',
'CompletedDownload': False, 'LinkData': {1: {'Link': 'www.yahoo.com',
'CompletedDownload': True}, 2: {'Link': 'www.google.com',
'CompletedDownload': False}}}}
我在 Windows 10 64 位上使用 Python 3.8。
BotAccountsLinksList[item].update(BotLinksList)
正在做
for k,v in BotLinksList.items():
BotAccountsLinksList[item][k] = v
BotLinksList
值将在多个 BotAccountsLinksList
键之间共享。不要多次重复使用相同的字典或进行深度复制
import copy
BotAccountsLinksList[item].update(copy.deepcopy(BotLinksList))