问题描述
- python 列表中的n个字典的更新问题
-
弄了一下午还是没想明白:
eg:
d1 = [{"eslid":"5A-15-D4-99","nw1":"51-02-01-66","nw3":"50",'rfpower': '50', 'netid': '65', 'apid': 1, 'version': '51', 'battery': '30', 'reverse': '0'}] * 10
d2 = [{"eslid":"5A-15-D4-99","nw1":"51-02-01-66","nw3":"50",'rfpower': '50', 'netid': '65', 'apid': 1, 'version': '51', 'battery': '30', 'reverse': '0'}, {"eslid":"5A-15-D4-99","nw1":"51-02-01-66","nw3":"50",'rfpower': '50', 'netid': '65', 'apid': 1, 'version': '51', 'battery': '30', 'reverse': '0'}, {"eslid":"5A-15-D4-99","nw1":"51-02-01-66","nw3":"50",'rfpower': '50', 'netid': '65', 'apid': 1, 'version': '51', 'battery': '30', 'reverse': '0'}, {"eslid":"5A-15-D4-99","nw1":"51-02-01-66","nw3":"50",'rfpower': '50', 'netid': '65', 'apid': 1, 'version': '51', 'battery': '30', 'reverse': '0'}, {"eslid":"5A-15-D4-99","nw1":"51-02-01-66","nw3":"50",'rfpower': '50', 'netid': '65', 'apid': 1, 'version': '51', 'battery': '30', 'reverse': '0'}, {"eslid":"5A-15-D4-99","nw1":"51-02-01-66","nw3":"50",'rfpower': '50', 'netid': '65', 'apid': 1, 'version': '51', 'battery': '30', 'reverse': '0'}, {"eslid":"5A-15-D4-99","nw1":"51-02-01-66","nw3":"50",'rfpower': '50', 'netid': '65', 'apid': 1, 'version': '51', 'battery': '30', 'reverse': '0'}, {"eslid":"5A-15-D4-99","nw1":"51-02-01-66","nw3":"50",'rfpower': '50', 'netid': '65', 'apid': 1, 'version': '51', 'battery': '30', 'reverse': '0'}, {"eslid":"5A-15-D4-99","nw1":"51-02-01-66","nw3":"50",'rfpower': '50', 'netid': '65', 'apid': 1, 'version': '51', 'battery': '30', 'reverse': '0'}, {"eslid":"5A-15-D4-99","nw1":"51-02-01-66","nw3":"50",'rfpower': '50', 'netid': '65', 'apid': 1, 'version': '51', 'battery': '30', 'reverse': '0'}]#分别对d1和d2进行操作 for i in range(10): d1[i]['eslid'] = L[i][0] d1[i]['nw1'] = L[i][1] d1[i]['nw3'] = L[i][2] #然后分别打印d1与d2 发现d1是最后更新的值 d2的值是正常更新的 为什么呢?
解决方案
http://blog.csdn.net/bolike/article/details/20402077
解决方案二:
python字典value为列表时遇到的问题
【python】列表的复制问题
时间: 2024-12-12 12:30:04