gpt4 book ai didi

Python 字典未正确更新

转载 作者:太空宇宙 更新时间:2023-11-04 08:52:12 24 4
gpt4 key购买 nike

mydict = {}        
bufferID = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26]
physicalAddrList1 = [9,8]
physicalAddrList2 = [10,11]
addressToIdMap = {}
def Update_Dictionary(physicalAddrList,cmdId,opcode):
for address in physicalAddrList:
list = GetBufferId()
addressToIdMap[address] = list
mydict[cmdId,opcode] = addressToIdMap
def GetBufferId():
list = []
list.append([bufferID.pop(),25])
return list

Update_Dictionary(physicalAddrList1,1,0x01)
Update_Dictionary(physicalAddrList2,2,0x02)
print mydict

输出:

C:\Users\29213\Desktop> list4.py
{(1, 1): {8: [[25, 25]], 9: [[26, 25]], 10: [[24, 25]], 11: [[23, 25]]}, (2, 2): {8: [[25, 25]], 9: [[26, 25]], 10: [[24, 25]], 11: [[23, 25]]}}

即使两个函数调用都是使用不同的物理地址列表进行的,但两个字典键都具有所有 4 个物理地址。
理想的输出应该是:

{(1, 1): {8: [[25, 25]], 9: [[26, 25]]}, (2, 2):{10: [[24, 25]], 11: [[23, 25]]}}

最佳答案

问题在于声明 addressToIdMap = {}。您在模块级别声明了它,因此在您第二次调用 Update_Dictionary 函数之前它不会被清除。

你需要把它移到 Update_Dictionary

def Update_Dictionary(physicalAddrList,cmdId,opcode): 
addressToIdMap = {}
for address in physicalAddrList:
list = GetBufferId()
addressToIdMap[address] = list
mydict[cmdId,opcode] = addressToIdMap

关于Python 字典未正确更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34036595/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com