【问题标题】:Combining two list into one using an equal variable使用相等变量将两个列表合并为一个
【发布时间】: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


【解决方案1】:

执行此操作的 Pythonic 方法是使用以接口名称为键的字典来收集组合信息,这将您的整个代码简化为:

# Creates an appended list containing interface name and ip
IPresponse = response['ietf-interfaces:interfaces']['interface']

combined_info = {}

for dev in IPresponse:
    combined_info[dev['name']] = {'ip': dev['ietf-ip:ipv4']}

# Creates a appended list containing interface name and MAC address
MACresponse = response['ietf-interfaces:interfaces-state']['interface']

for dev in MACresponse:
    combined_info[dev['name']]['MAC Address'] = dev['phys-address']

请注意,这假设所有相同的接口名称都出现在 IPresponse 和 MACresponse 中,看起来就是这种情况。

要在功能上与您的代码等效并且只收集同时具有 IP 地址和 MAC 的值,您可以在最后添加一些额外的逻辑和过滤。

# Creates an appended list containing interface name and ipIPresponse = response['ietf-interfaces:interfaces']['interface']

combined_info = {}

for dev in IPresponse:
    combined_info[dev['name']] = {'ip': dev['ietf-ip:ipv4']}

# Creates a appended list containing interface name and MAC address
MACresponse = response['ietf-interfaces:interfaces-state']['interface']

for dev in MACresponse:
    # This check is optional, depends on the nature of response
    devname = dev['name']
    if devname in combined_info:
        combined_info[devname]['MAC Address'] = dev['phys-address']

# another optional cleanup
combined_info = {devname, info for devname, info in combined_info.items() if 'MAC Address' in info}

PS:可能值得一看PEP 8

【讨论】:

    猜你喜欢
    • 2021-10-19
    • 1970-01-01
    • 1970-01-01
    • 2014-01-27
    • 2014-04-01
    • 2014-03-28
    • 2018-11-27
    相关资源
    最近更新 更多