我使用下面的代码获得了意想不到的结果。我试图更新字典中包含的列表。我在Windows 10上使用python 3.6.4。
discovery = {'interface': {}}
interface_sch = {
'type':'',
'etherchannel':'',
'description':'',
'tagged':[],
'untagged':'',
'ipaddress':'',
'portstate':'',
'linkstate':'',
'speed':'',
'remotedevice':'',
'remotedeviceport':'',
}
#Populate {interface:} key(1/1 - 1/48)
for port in range(1,49):
port=str(port)
discovery['interface']["1/" + port]=interface_sch
#Populate port tag list.
for tag in ['1','2','10','20']:
for port in ['1/1','1/3','1/5','1/10']:
discovery['interface'][port]['tagged'].append(tag)
#Sample one of the interfaces from the dictionary
print(discovery['interface']['1/3']['tagged'])
为什么我的结果? [' 1',' 1',' 1',' 1',' 2',' 2',' 2',' 2',' 10',' 10',' 10', ' 10',' 20',' 20',' 20',' 20']