You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

嵌套列表遍历编辑及ARP表数据提取技术问题求助

Solutions to Your Two Technical Issues

Hey there! Let's work through your problems step by step—you’re already ahead by reading the file into a list with with/as and readlines(), so let’s build on that.

1. Traversing and Editing Nested Lists

It sounds like your main headache was index shifting when trying to remove elements, which is a super common pitfall. Let’s cover two reliable approaches:

Option 1: Create a New List with Only Needed Elements

Instead of modifying the original nested list in place (which can mess up indexes as elements shift), build a new list that keeps only the IP (index 1) and MAC (index 3) elements directly:

# Assume your original nested list is named arp_entries
cleaned_entries = []
for entry in arp_entries:
    # Extract index 1 (IP) and 3 (MAC) to form a new sublist
    cleaned_subentry = [entry[1], entry[3]]
    cleaned_entries.append(cleaned_subentry)

You can shorten this with a list comprehension for brevity (and it’s just as readable):

cleaned_entries = [[entry[1], entry[3]] for entry in arp_entries if len(entry) >= 4]

The len(entry) >=4 check prevents index errors if any sublists are shorter than expected.

Option 2: Modify the Original List Safely

If you need to edit the nested list in place, avoid deleting elements in forward order—this shifts remaining elements and makes your target indexes wrong. Instead, delete indexes in reverse order:

for entry in arp_entries:
    # Delete indexes 0,2,4,5 starting from the end to avoid index shifts
    for idx in reversed([0,2,4,5]):
        if idx < len(entry):  # Make sure the index exists in the sublist
            del entry[idx]

This way, deleting a higher index doesn’t affect the positions of lower indexes you still need to remove.

2. Extracting IP and MAC from ARP Table Data

Since you already have the file content in a list via readlines(), first we need to convert each line into a structured sublist. ARP tables usually have whitespace-separated values, so we’ll split each line and clean up extra spaces:

arp_entries = []
with open('arp_table.txt', 'r') as f:
    lines = f.readlines()
    # Skip header lines if your ARP table has them (adjust the slice as needed)
    for line in lines[1:]:
        # Split line into parts, stripping whitespace and filtering empty strings
        parts = [segment.strip() for segment in line.split() if segment.strip()]
        if parts:  # Skip empty lines
            arp_entries.append(parts)

# Now extract IP and MAC using the method from the first issue
ip_mac_pairs = [[entry[1], entry[3]] for entry in arp_entries if len(entry) >=4]

If your ARP table uses a different format (like commas instead of spaces), just replace split() with split(',') and adjust the indexes if the column order is different.


内容的提问来源于stack exchange,提问作者biddle370018

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:15:38