我尽了最大努力想弄清楚,但我一辈子都搞不清楚。我有一本字典,里面有很多不同的值,包括另一本字典。在为字典中的字典设置值之前,我尝试将值设置为“空白”字典,以便在后续步骤中更新它。你知道吗
简短的故事是:我有两行字,不知怎的改变了一本字典,这是我意想不到的。给出一些格言:
blankresiduedict = {stuff:[stuff], stuff:[stuff]}; blankidentifiers = {stuff:stuff, stuff:stuff}
台词
self.pdb_chain_dict['1MH9_B'] = (blankresiduedict.copy(),blankidentifiers.copy())
self.pdb_chain_dict['1MH9_B'][0][(stuff)][0] = residuedict[('A','D','41')]
正在以某种方式更改blankresiduedict的值,使之等于residuedict。你知道吗
这是一个非常大的项目的一小部分,所以其中一些可能很难用紧凑的形式表示。我会尽力消除不必要的东西。这是一个类中的方法,我正试图用它来更新类实例的字典。你知道吗
blankresiduedict = {}
blankidentifiers = {}
self.allowmultiples = True
self.ancestorline = [
'1MH9',
'A', 'D', '41',
'A', 'D', '43',
'A', 'T', '130',
#etc...
]
self.no_key_residues = 6
self.pdb_chain_dict = {
'1MH9_B': (
{
('A','D','41'): [('B','D','41')],
('A','D','43'): [('B','D','43')],
('A','T','130'): [('B','T','130')]
},
#{identifiers dictionary}
),
'1MH9_C': (
#{etc},{etc}
),
# etc...
}
for i in range(1, (3*self.no_key_residues)+1, 3): # Using this loop structure allows a variable number of key residues to be given
if not self.allowmultiples:
raise Exception("Do some stuff here")
else:
blankresiduedict[(self.ancestorline[i],self.ancestorline[i+1],self.ancestorline[i+2])] = [('-','-','-')]
blankidentifiers = {'EC Num':'-','Sprot':'-','Class':'-','Keywords':'-','Title':'-','SeqRepr':'-'}
### Begin some loop structure, where for every loop, the following is basically happening
residuedict = {
('A','D','41'): ('B','D','10'),
('A','D','43'): ('B','D','12')
} #in actuality this value would change for every loop, but just showing what a typical loop would look like
self.pdb_chain_dict['1MH9_B'] = (blankresiduedict.copy(),blankidentifiers.copy())
self.pdb_chain_dict['1MH9_B'][0][('A','D','41')][0] = residuedict[('A','D','41')]
这里应该发生的是,pdb\u chain\u dict中的值被设置为两个空白字典({residuedict},{identifiers})的元组。在这个例子中,我基本上不使用标识符字典,因为它有完全相同的问题。然而,我发现blankresiduedict实际上正在改变。而且,在做了大量的测试之后,它改变的地方是self.pdb_chain_dict['1MH9_B'][0][('A,'D','41')][0] = residuedict[('A','D','41')]
。你知道吗
这对我来说毫无意义…blankresiduedict甚至没有参与,但不知怎么的,它的值在这一步中被改变了。你知道吗
这是因为字典的副本不是深度副本,而dict值是可变的列表。下面是一个再现您的问题的最小示例:
相关问题 更多 >
编程相关推荐