初学,一个字典列表的问题
newusers = []users = {
'XXX': [] ,
'YYY': [],
}
for i in range(110,112):
print(i)
users['XXX'] = i
users['YYY'] = i + 100
print('this is users', users, )
print('this is newusers befor appended', str(i), newusers)
newusers.append(users)
print('this newusers appended ', str(i), newusers)
print('\n')
执行结果如下
110
this is users {'XXX': 110, 'YYY': 210}
this is newusers befor appended 110 []
this newusers appended 110 [{'XXX': 110, 'YYY': 210}]
111
this is users {'XXX': 111, 'YYY': 211}
this is newusers befor appended 111 [{'XXX': 111, 'YYY': 211}] 这里为什么不是[{'XXX': 110, 'YYY': 210}] ?
this newusers appended 111 [{'XXX': 111, 'YYY': 211}, {'XXX': 111, 'YYY': 211}] 我期待的结果是 [{'XXX': 110, 'YYY': 210}, {'XXX': 111, 'YYY': 211}]
那里错了 ?
[此贴子已经被作者于2019-3-23 11:11编辑过]