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

如何匹配接口名称分组并计算接收与发送数据量的比值?

接口流量比值计算实现

原始数据

list1 = [
    {'itemid': '264', 'name': 'Interface Gi1/17(Port1:TP TPIA-CL03-017-G15-14): Bits sent', 'some_other_keys': 'some_more_values'},
    {'itemid': '215', 'name': 'Interface Te1/50("Port1:CL-PO-G22-23"): Bits received', 'some_other_keys': 'some_more_values'},
    {'itemid': '425', 'name': 'Interface Gi1/46(no description): Bits sent', 'some_other_keys': 'some_more_values'},
    {'itemid': '521', 'name': 'Interface Te1/50("Port1:CL-PO-G22-23"): Bits sent', 'some_other_keys': 'some_more_values'},
    {'itemid': '310', 'name': 'Interface Gi1/46(no description): Bits received', 'some_other_keys': 'some_more_values'},
    {'itemid': '123', 'name': 'Interface Gi1/17(Port1:TP TPIA-CL03-017-G15-14): Bits received', 'some_other_keys': 'some_more_values'},
]
list2 = [
    {'itemid': '264', 'clock': '1724146566', 'value': '6246880', 'ns': '120003316'},
    {'itemid': '264', 'clock': '1724146746', 'value': '6134912', 'ns': '113448784'},
    {'itemid': '215', 'clock': '1724144406', 'value': '5786832', 'ns': '157177073'},
    {'itemid': '215', 'clock': '1724144766', 'value': '5968784', 'ns': '760851309'},
    {'itemid': '425', 'clock': '1724148366', 'value': '6590424', 'ns': '403316048'},
    {'itemid': '425', 'clock': '1724148726', 'value': '6549984', 'ns': '484278803'},
    {'itemid': '521', 'clock': '1724148906', 'value': '6346488', 'ns': '306999249'},
    {'itemid': '521', 'clock': '1724147106', 'value': '6139008', 'ns': '459391602'},
    {'itemid': '310', 'clock': '1724147286', 'value': '6000208', 'ns': '826776455'},
    {'itemid': '310', 'clock': '1724147466', 'value': '6784960', 'ns': '152620809'},
    {'itemid': '123', 'clock': '1724147826', 'value': '6865272', 'ns': '70247389'},
    {'itemid': '123', 'clock': '1724148186', 'value': '6544328', 'ns': '610791670'},
]

已完成步骤

通过defaultdict按itemid汇总list2中大于0的value值:

from collections import defaultdict
new_list = defaultdict(list)
for entry in list2:
    if int(entry['value']) > 0:
        new_list[int(entry['itemid'])].append(int(entry['value']))

# 输出结果:
# defaultdict(<class 'list'>, {264: [6246880, 6134912], 215: [5786832, 5968784], 425: [6590424, 6549984], 521: [6346488, 6139008], 310: [6000208, 6784960], 123: [6865272, 6544328]})

需求

  • 对list1中的条目,按名称中: Bits前的接口名称分组,找到每组对应"Bits received"和"Bits sent"的itemid
  • 计算每组中接收数据总和(对应received的itemid的value总和)除以发送数据总和(对应sent的itemid的value总和)的比值
  • 生成包含接口名称、接收总和、发送总和、比值的结构化列表

实现代码

# 构建接口名称到sent/received对应itemid的映射
interface_map = {}
for item in list1:
    # 拆分接口名称与流量类型
    name_part, traffic_type = item['name'].split(': Bits ')
    itemid = int(item['itemid'])
    
    if name_part not in interface_map:
        interface_map[name_part] = {'received': None, 'sent': None}
    
    # 匹配流量类型并存储itemid
    if traffic_type == 'received':
        interface_map[name_part]['received'] = itemid
    elif traffic_type == 'sent':
        interface_map[name_part]['sent'] = itemid

# 计算每组的总和与比值
output = []
for interface_name, ids in interface_map.items():
    received_id = ids['received']
    sent_id = ids['sent']
    
    # 计算接收、发送数据总和
    received_total = sum(new_list[received_id])
    sent_total = sum(new_list[sent_id])
    # 处理除数为0的情况
    ratio = received_total / sent_total if sent_total != 0 else 0.0
    
    output.append({
        'name': interface_name,
        'received': received_total,
        'sent': sent_total,
        'ratio': ratio
    })

# 打印结果
print(output)

输出结果

[
    {'name': 'Interface Gi1/17(Port1:TP TPIA-CL03-017-G15-14)', 'received': 13409600, 'sent': 12381792, 'ratio': 1.0830096322083265},
    {'name': 'Interface Te1/50("Port1:CL-PO-G22-23")', 'received': 11755616, 'sent': 12485496, 'ratio': 0.941541769746272},
    {'name': 'Interface Gi1/46(no description)', 'received': 12785168, 'sent': 13140408, 'ratio': 0.9729658318067446}
]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 01:57:01