我很困惑,为什么字典键45.0
没有在cmd_gear.states()
中被替换,而是在dictionary
中被替换:
print 'original cmd_gear.states() = '
pprint.pprint(cmd_gear.states())
cmd_gear.states()['N'] = cmd_gear.states()[45.0]
del cmd_gear.states()[45.0]
print '\nreplaced cmd_gear.states() = '
pprint.pprint(cmd_gear.states())
dictionary = {
6.0: [[288.031, 441.971]],
45.0: [[0.211, 6.071], [34.101, 69.671], [540.021, 644.761]]}
dictionary['N'] = dictionary[45.0]
del dictionary[45.0]
print '\nreplaced dictionary'
pprint.pprint(dictionary)
以下是输出:
original cmd_gear.states() =
{
6.0: [[288.031, 441.971]],
45.0: [[0.211, 6.071], [34.101, 69.671], [540.021, 644.761]]}
replaced cmd_gear.states() =
{
6.0: [[288.031, 441.971]],
45.0: [[0.211, 6.071], [34.101, 69.671], [540.021, 644.761]]} # this is what confuses me!!! 45.0 should be 'N'
replaced dictionary
{
6.0: [[288.031, 441.971]],
'N': [[0.211, 6.071], [34.101, 69.671], [540.021, 644.761]]}
我真的很困惑为什么它不能用N代替45.0
正如@chepner所评论的,问题是,每次我都会得到一本新字典。必须使变量等于
cmd_gear.states()
,否则将无法修改它。以下是解决方案:相关问题 更多 >
编程相关推荐