我错过了什么?我有一个像这样的字典(它是动态创建的):
googlers = 3
goog_dict = {}
dict_within = {'score':[], 'surprise':''}
for i in xrange(googlers):
name = "goog_%s" %i
goog_dict[name] = dict_within
现在我想添加一些数据:
tot =[23,22,21]
best_res = 8
for i in xrange(len(tot)):
name = "goog_%s" %i
print name
rest = tot[i] - best_res
if rest % 2 == 0:
trip = [best_res, rest/2, rest/2]
elif rest % 2 != 0:
rest_odd = rest / 2
fract_odd = rest - rest_odd
trip = [best_res, rest_odd, fract_odd]
if (max(trip) - min(trip)) == 2:
surpr_state = True
elif (max(trip) - min(trip)) < 2:
surpr_state = False
goog_dict[name]['score'].append(trip)
goog_dict[name]['surprise'] = surpr_state
我希望我的输出是:
{'goog_2': {'surprise': True, 'score': [8, 7, 8]}, 'goog_1':{'surprise': True, 'score': [8, 7, 7]}, 'goog_0': {'surprise': True, 'score': [8, 6, 7]}}
但我得到的是:
{'goog_2': {'surprise': True, 'score': [[8, 7, 8], [8, 7, 7], [8, 6, 7]]}, 'goog_1':{'surprise': True, 'score': [[8, 7, 8], [8, 7, 7], [8, 6, 7]]}, 'goog_0': {'surprise': True, 'score': [[8, 7, 8], [8, 7, 7], [8, 6, 7]]}}
那么,为什么列表会trip
附加到所有dicts 而不是仅具有 current 的一个name
?