无法将新列表数据附加到for循环中的dict

发布于 2025-02-03 13:34:38 字数 2437 浏览 2 评论 0原文

我有一个从ListIP中的帖子返回的IP列表。我想迭代IP的列表并将数据存储在字典中,以便我可以在网页上渲染。但是字典仅覆盖了最后一个IP的值。我该如何解决?目前,ListIP中有3个IP,但DICE仅存储最后一个通过的IPS数据。

def healthcheckresults(request):
if not listip:
    return render(request, "home/homepage.html",)
for ip in range(len(listip)):
    conn = manager.connect(
    host= listip[ip],
    port='22',
    username='XXX',
    password = 'XXX',
    timeout=10
    )
    result = conn.get_ospf_neighbor_information()
    hostnameresult = conn.get_software_information()
    hostname = hostnameresult.xpath('//software-information/host-name/text()')
    ospfneighboraddress = result.xpath('//ospf-neighbor/neighbor-address/text()')
    ospfneighborinterface = result.xpath('//ospf-neighbor/interface-name/text()')
    ospfneighborstate= result.xpath('//ospf-neighbor/ospf-neighbor-state/text()')
    ospfneighborID = result.xpath('//ospf-neighbor/neighbor-id/text()')
    
    ##METHOD1
    ospfdictkey = {"hostname":[],"ospfneighboraddress":[],"ospfneighborinterface":[],"ospfneighborstate":[],"ospfneighborID":[]}
    ospfmetalist = [hostname,ospfneighboraddress,ospfneighborinterface,ospfneighborstate,ospfneighborID]
    for key, value in zip(ospfdictkey, ospfmetalist):
        ospfdictkey[key].append(value)
        
    ##METHOD2
    ospfdict={"hostname":hostname,"ospfneighboraddress":ospfneighboraddress,"ospfneighborinterface":ospfneighborinterface, "ospfneighborstate":ospfneighborstate,"ospfneighborID":ospfneighborID }
    context = {'LUnique': zip(ospfneighboraddress, ospfneighborinterface, ospfneighborstate,ospfneighborID)}
    conn.close_session()

listip.clear()
return render(request, "healthcheck/healthcheckresults.html",{
    "ospfneighboraddress":ospfneighboraddress,
    "ospfneighborinterface":ospfneighborinterface,
    "ospfneighborstate":ospfneighborstate,
    "ospfneighborID":ospfneighborID,
    "context":context,
    "hostname":hostname,
    "listip":listip,
    "ospfdict":ospfdict,
    "ospfdictkey":ospfdictkey,
})

当我检查字典中的数据时,两种提到的方法都返回相同的数据。

{'hostName':['r3-isp'],'ospfneighboraddress':['192.168.5.34','192.168.5.5','192.168.5.10'],'ospfneighborterface': .0','ae3.0'],'ospfneighborstate':['full','full',full'],'ospfneighborid':['172.0.0.6','172.0.0.0.2','172.0.0.0.0.0.0.44 ']}

{'hostName':[['r3-isp']],'ospfneighboraddress':[['192.168.5.34','192.168.5.5','192.168.5.10'] 'ae10.0','ae2.0','ae3.0']] ','172.0.0.2','172.0.0.4']]} ['r3-isp']

I have a list of IPs being returned from a POST in listip. I want to iterate over the list of IPs and store data in a dictionary so i can render it on a webpage. But the dictionary is overriding the values for the last IP only. How can i solve this ? Currently there are 3 IPs in the listip but dict is only storing the last passed IPs data.

def healthcheckresults(request):
if not listip:
    return render(request, "home/homepage.html",)
for ip in range(len(listip)):
    conn = manager.connect(
    host= listip[ip],
    port='22',
    username='XXX',
    password = 'XXX',
    timeout=10
    )
    result = conn.get_ospf_neighbor_information()
    hostnameresult = conn.get_software_information()
    hostname = hostnameresult.xpath('//software-information/host-name/text()')
    ospfneighboraddress = result.xpath('//ospf-neighbor/neighbor-address/text()')
    ospfneighborinterface = result.xpath('//ospf-neighbor/interface-name/text()')
    ospfneighborstate= result.xpath('//ospf-neighbor/ospf-neighbor-state/text()')
    ospfneighborID = result.xpath('//ospf-neighbor/neighbor-id/text()')
    
    ##METHOD1
    ospfdictkey = {"hostname":[],"ospfneighboraddress":[],"ospfneighborinterface":[],"ospfneighborstate":[],"ospfneighborID":[]}
    ospfmetalist = [hostname,ospfneighboraddress,ospfneighborinterface,ospfneighborstate,ospfneighborID]
    for key, value in zip(ospfdictkey, ospfmetalist):
        ospfdictkey[key].append(value)
        
    ##METHOD2
    ospfdict={"hostname":hostname,"ospfneighboraddress":ospfneighboraddress,"ospfneighborinterface":ospfneighborinterface, "ospfneighborstate":ospfneighborstate,"ospfneighborID":ospfneighborID }
    context = {'LUnique': zip(ospfneighboraddress, ospfneighborinterface, ospfneighborstate,ospfneighborID)}
    conn.close_session()

listip.clear()
return render(request, "healthcheck/healthcheckresults.html",{
    "ospfneighboraddress":ospfneighboraddress,
    "ospfneighborinterface":ospfneighborinterface,
    "ospfneighborstate":ospfneighborstate,
    "ospfneighborID":ospfneighborID,
    "context":context,
    "hostname":hostname,
    "listip":listip,
    "ospfdict":ospfdict,
    "ospfdictkey":ospfdictkey,
})

Both mentioned methods are returning the same data when i check the data in the dictionary.

{'hostname': ['R3-ISP'], 'ospfneighboraddress': ['192.168.5.34', '192.168.5.5', '192.168.5.10'], 'ospfneighborinterface': ['ae10.0', 'ae2.0', 'ae3.0'], 'ospfneighborstate': ['Full', 'Full', 'Full'], 'ospfneighborID': ['172.0.0.6', '172.0.0.2', '172.0.0.4']}

{'hostname': [['R3-ISP']], 'ospfneighboraddress': [['192.168.5.34', '192.168.5.5', '192.168.5.10']], 'ospfneighborinterface': [['ae10.0', 'ae2.0', 'ae3.0']], 'ospfneighborstate': [['Full', 'Full', 'Full']], 'ospfneighborID': [['172.0.0.6', '172.0.0.2', '172.0.0.4']]}
['R3-ISP']

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风尘浪孓 2025-02-10 13:34:38

在每种方法中,您都在覆盖dict。请记住,在每次迭代中,代码都在重复,

for ip in range(len(listip)):

这意味着当您在每种方法的第一行中设置键和值时,您正在覆盖以前使用的任何名称的任何dist。

避免这种情况的一种方法是创建一个空列表,并在创建时附加每个新列表。然后,您可以循环浏览列表以查看每个dict。

aList = []
for ip in range(len(listip)):
...
#Method1
...
aList.append(ospfdictkey )
#Method2
...
aList.append(ospfdict)

In each method you are overwriting the dict. Remember that the code is repeating in each iteration of

for ip in range(len(listip)):

Which means that when you set the keys and values in the first line of each method you are overwriting any dict of that name that has gone before.

One way to avoid this would be to create an empty list, and append each new dict to it upon creation. Then you can cycle through the list to see each dict.

aList = []
for ip in range(len(listip)):
...
#Method1
...
aList.append(ospfdictkey )
#Method2
...
aList.append(ospfdict)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文