简体   繁体   English

使用 for 循环迭代嵌套列表以编辑内部列表时遇到问题

[英]Trouble iterating through a nested list using a for loop to edit the inner lists

I just can't figure out how to manage some arp tables.我就是不知道如何管理一些 arp 表。 The only data that is relevant to me is the ip address and the mac address listed.唯一与我相关的数据是列出的 ip 地址和 mac 地址。 I don't know if I should,不知道该不该

  1. iterate through the nested_list , removing [0, 2, 4, 5]遍历nested_list ,删除[0, 2, 4, 5]

  2. iterate through the lists, assigning [1] and [3] to new lists.遍历列表,将[1][3]分配给新列表。

I have tried both ways and I'm just not getting there.我已经尝试了两种方法,但我只是没有到达那里。 My syntax knowledge is clearly lacking.我的语法知识显然缺乏。

Before any of the code you're about to see I start by accessing a file using with / as and use .readlines() to create a list of strings I call read_output .在您将要看到的任何代码之前,我首先使用with / as访问文件as并使用.readlines()创建一个我称之为read_output的字符串列表。

Here is my code as it stands now:这是我现在的代码:

nested_list = []

for index in read_output: 
    nested_list.append(index.split()) #this creates the nested list.

print(nested_list)

Which works fine and gives me this output as example:哪个工作正常,并以这个输出为例:

[['Internet', '10.220.88.1', '135', '0062.ec29.70fe', 'ARPA', 'FastEthernet4'],
 ['Internet', '10.220.88.40', '144', '001c.c4bf.826a', 'ARPA', 'FastEthernet4']]

The arp list can be a lot bigger than this but I thought I would save some space for our example. arp 列表可以比这大很多,但我想我会为我们的示例节省一些空间。 I want to be able to go through the list and add/remove/extract data as I see fit but every time I try I am met with varying degrees of failure.我希望能够浏览列表并按照我认为合适的方式添加/删除/提取数据,但每次尝试时都会遇到不同程度的失败。

If i just:如果我只是:

print(nested_list[0][3])

I get the expected result of 0062.ec29.70fe but when I try to use a second for loop to access every [n][0] , [n][2] , etc., things go horribly awry.我得到了0062.ec29.70fe的预期结果,但是当我尝试使用第二个 for 循环访问每个[n][0][n][2]等时,事情变得非常糟糕。

I'm starting with:我从:

for index in read_output:
    nested_list.append(index.split())
    for indx in nested_list:
    # --->insert some horrible excuse for python here

My intended output is taking the nested list above and trimming it down to:我的预期输出是采用上面的嵌套列表并将其修剪为:

[['10.220.88.1', '0062.ec29.70fe'],
 ['10.220.88.40', '001c.c4bf.826a']]

Don't append the whole sub-list in the first place but only the two items you want:首先不要附加整个子列表,而只附加您想要的两个项目:

nested_list = []

for index in read_output: 
    inner = index.split()
    nested_list.append([inner[1], inner[3]]) 

Alright so I hope I'm understanding this correctly, but from what I understand, you're getting your data correctly, which is this:好吧,所以我希望我能正确理解这一点,但据我所知,您正确地获取了数据,这是:

[['Internet', '10.220.88.1', '135', '0062.ec29.70fe', 'ARPA', 
'FastEthernet4'],
 ['Internet', '10.220.88.40', '144', '001c.c4bf.826a', 'ARPA', 
'FastEthernet4']]

So you're correct in seeing that this is actually a list, containing two sub-lists.所以你看到这实际上是一个列表,包含两个子列表是正确的。 We can take advantage of this in our code below.我们可以在下面的代码中利用这一点。

list = [['Internet', '10.220.88.1', '135', '0062.ec29.70fe', 'ARPA', 'FastEthernet4'],['Internet', '10.220.88.40', '144', '001c.c4bf.826a', 'ARPA', 'FastEthernet4']]

So now that we've contained that list, similar to how you did, we can loop through that list.所以现在我们已经包含了该列表,类似于您所做的,我们可以遍历该列表。

for item in list:
    print ("IP: {0}\nMac Address:{1}\n".format(item[1],item[3]))

This gave me the output that I hope you're expecting which is:这给了我希望你期待的输出:

IP: 10.220.88.1
Mac Address: 0062.ec29.70fe

IP: 10.220.88.40
Mac Address: 001c.c4bf.826a

So what we did is run a loop through the outer list, and since we knew that the IP address was always going to be at index of 1 and 3 of the inner list, we just accessed them through their index.所以我们所做的是在外部列表中运行一个循环,因为我们知道 IP 地址总是位于内部列表的索引 1 和 3 处,我们只是通过它们的索引访问它们。

You don't need to run another loop through the inner lists since you already have this knowledge of their placement.您不需要通过内部列表运行另一个循环,因为您已经了解它们的位置。

To store these addresses you'd probably want to store them in a dictionary since you have a key, value pair kind of setup.要存储这些地址,您可能希望将它们存储在字典中,因为您有一个键值对类型的设置。

address = {}
count += 1
for item in list:
    count += 1
    address["list: {0}".format(count)] = [item[1], item[3]]
    print ("IP: {0}".format(item[1])
    print ("Mac Address: {0}".format(item[3]))
    print ()
print (address)

With this we set a count to increase our key name, and with each new list, we saved the two specific values we wanted in another list.有了这个,我们设置了一个计数来增加我们的键名,并且对于每个新列表,我们将我们想要的两个特定值保存在另一个列表中。 A good challenge would be to see how you could convert it to a form where your key is the ip and mac and your values are, well the values.一个很好的挑战是看看如何将其转换为密钥是 ip 和 mac 并且您的值是值的形式。 I thought about doing that but it even boggled my mind a bit and I wanted to get my answer out to you quickly.我想这样做,但它甚至让我有点困惑,我想尽快给你答案。 Hope this helps!希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM