【发布时间】:2021-05-17 12:50:35
【问题描述】:
到目前为止,我在创建组合列表时遇到问题,我从响应值创建了两个列表。像下面这样
这是我的IP地址列表
# Creates an appended list containing interface name and ip
IPresponse = response['ietf-interfaces:interfaces']['interface']
global IPList
IPList = []
for dev in IPresponse:
IPList.append(
{'Interface Name': dev['name'], 'Ip Address': dev['ietf-ip:ipv4']})
print(IPList)
return IPList
这是 MAC 地址列表。我想合并这两个列表。
# Creates a appended list containing interface name and MAC address
MACresponse = response['ietf-interfaces:interfaces-state']['interface']
global MAClist
MAClist = []
for dev in MACresponse:
MAClist.append({'Interface Name': dev['name'],
'Physical Address': dev['phys-address']})
print(MAClist)
return MAClist
我如何组合列表如下:
def combinedlist(IPList, MAClist):
combinedlist = ['']
for ipdict in IPList:
for macdict in MAClist:
if ipdict['Interface Name'] == macdict['Interface Name']:
combinedlist.append({'name': ipdict['Interface Name'],
'ip': ipdict['Ip Address'],
'MAC Address': macdict[
'Physical Address']})
print(combinedlist)
我正在尝试通过使用接口名称将它们组合起来,以确保每个接口的正确信息都与相应的接口对应。现在我遇到了一个错误,上面写着 “function object not iterable”,但总的来说,我认为这有点混乱,想知道任何人都可以提供帮助。
谢谢
【问题讨论】:
-
这是什么语言?
-
python3.9 很抱歉没有澄清
-
你为什么要使用列表?保留接口名称的单一字典将使所有这一切变得容易得多。如果排序很重要,您可以拥有一个字典并添加一个“索引”成员,当您将新成员添加到外部字典时,该成员会单调递增。
-
因为我正在从 cisco 交换机中提取这些列表并尝试迭代接口
标签: python list dictionary