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

从接口配置字典的键子串中获取机箱总数及每机箱接口数

Solution: Count Chassis & Interfaces Efficiently

Got it, let's break this down efficiently. You need two key pieces of info without unnecessary iterations: total chassis count, and the number of interfaces per chassis. Here's a clean, efficient approach using Python (perfect for dictionary manipulation like this):

Step 1: Structure the Data & Track Counts in One Pass

We'll iterate through the interface keys exactly once, split each valid key into its chassis and interface components, and build a count tracker as we go. This avoids redundant loops and stays fast even with huge numbers of interfaces.

First, let's translate your provided interface data into a proper Python dictionary (truncating repetitive entries for brevity—logic works for the full dataset):

# Your interface data converted to a Python dict
interfaces = {
    "1": {"tagged": []},  # Outlier entry—we'll filter it out
    "1/1": {"adminState": False, "lldp": False, "loopback": True, "mobile": True, "speed": '100'},
    "1/2": {"lldp": False, "loopback": True, "mobile": True, "speed": '100', "untagged": '100'},
    # ... All 1/3 to 1/49 entries here
    "1/50": {"lldp": False},
    "2/1": {"lldp": False, "loopback": True, "mobile": True, "speed": '100', "untagged": '200'},
    # ... All 2/2 to 2/49 entries here
    "2/50": {"alias": '->sw01-2/1/26', "linkagg": '1', "trap": True},
    "3/1": {"untagged": '700'}
}

# Tracker for interface counts per chassis
chassis_interface_counts = {}

# Loop through each interface key ONCE
for iface_key in interfaces:
    # Skip non-standard keys that don't follow "chassis/interface" format
    if "/" not in iface_key:
        continue
    # Split key into chassis ID and interface number (split once to avoid edge cases)
    chassis_id, _ = iface_key.split("/", 1)
    # Update count—use get() to handle new chassis automatically
    chassis_interface_counts[chassis_id] = chassis_interface_counts.get(chassis_id, 0) + 1

# Calculate and display results
total_chassis = len(chassis_interface_counts)
print(f"Total number of chassis: {total_chassis}")
print("\nInterface count per chassis:")
for chassis, count in chassis_interface_counts.items():
    print(f"Chassis {chassis}: {count} interfaces")

Step 2: Expected Output

Running this with your full dataset will give you:

  • Total chassis count: 3 (covers chassis 1, 2, and 3)
  • Interface counts:
    • Chassis 1: 50 interfaces (1/1 to 1/50)
    • Chassis 2: 50 interfaces (2/1 to 2/50)
    • Chassis 3: 1 interface (3/1)

Why This Works Efficiently

  • Single pass through the interface keys—no nested loops or redundant checks.
  • dict.get() simplifies initializing counts for new chassis without extra conditionals.
  • The "/" filter ensures we ignore invalid keys that don't match the expected naming pattern.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 06:55:53