起因,处理数据的时候遇到了下述问题:
dic = {}
l_new = []
l = [["没啥用的"],[{'q':1},{'q':2}]]
for i in l[1]:
dic.update({'new_q':i['q']})
l_new.append(dic)
print(l_new)
# 理想状态:[{'new_q': 1}, {'new_q': 2}]
# 实际状态:[{'new_q': 2}, {'new_q': 2}],这里是被覆盖了
原因是append的是字典地址,append(dic_address, dic_address),所以里面的内容随着dic更新自动更新。因此,dic定义不能写在for循环外面。