AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 78913527
Accepted
Saeed
Saeed
Asked: 2024-08-26 16:47:17 +0800 CST2024-08-26 16:47:17 +0800 CST 2024-08-26 16:47:17 +0800 CST

如何找到以某些字符串结尾的条件名称并将它们分开

  • 772

我有以下清单:

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'},
]

现在我要这样做:

首先,根据itemid存储每个值的总和和平均值(例如,对于264,将其所有值相加,然后除以 len() 函数):list2valueitemid

其次,找到其中的每两个itemidslist1并查看它们的名称是否匹配:

第三,将具有的值received除以sent。

我将所有相同的值添加itemid如下:

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']))

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

现在我应该查找 id 为 264 的name(name.split(': Bits')[0]事实上)是否list1与哪个 id 匹配,然后除以.endswith('Bits received') / .endswith('Bits sent')。

我们假设一下:

id 264 的名称是 Interface Gi1/17(Port1:TP TPIA-CL03-017-G15-14),另一个具有此名称的 id 是 123。现在,我们将 id 为 123 的所有值的总和(因为它是received)除以 id 为 264 的所有值的总和(因为它是sent)。

应该是这样的:

    [
        {'name': 'Interface Gi1/17(Port1:TP TPIA-CL03-017-G15-14)', 'received': 13409600, 'sent': 12381792, 'ratio': 1.0830096322083265} # ( (6865272 + 6544328) / (6246880 + 6134912) )
    ]

使用此代码,我可以对每个值求和并计算平均值itemid(但这不是我要做的):

import datetime
for x in new_list:
    print(f"Item ID: {x}, Ratio: {float(str(sum(new_list[x]) / len(new_list[x]) / 1000 / 1000)[:3])}")

电流输出:

Item ID: 264, Ratio: 6.1
Item ID: 215, Ratio: 5.8
Item ID: 425, Ratio: 6.5
Item ID: 521, Ratio: 6.2
Item ID: 310, Ratio: 6.3
Item ID: 123, Ratio: 6.7

完全预期的输出:

output = [
    {'name': 'Interface Gi1/17(Port1:TP TPIA-CL03-017-G15-14)', 'received': 13409600, 'sent': 12381792, 'ratio': 1.0830096322083265}, # ( (6865272 + 6544328) / (6246880 + 6134912) )
    {'name': 'Interface Te1/50("Port1:CL-PO-G22-23")', 'received': 11755616, 'sent': 12485496, 'ratio': 0.941541769746272}, # ( (5786832 + 5968784) / (6346488 + 6139008) )
    {'name': 'Interface Gi1/46(no description)', 'received': 12785168, 'sent': 13140408, 'ratio': 0.9729658318067446}, # ( (6000208 + 6784960) / (6590424 + 6549984) )
]

你能帮我怎样到达那里吗?

python
  • 1 1 个回答
  • 47 Views

1 个回答

  • Voted
  1. Best Answer
    Sasanka Weerakoon
    2024-08-26T17:44:21+08:002024-08-26T17:44:21+08:00

    为了获得这样的输出,

    [{'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}]
    

    您必须在用比例计算后映射到输出

    for key, values in matches.items():
        output.append({
            'name': key,
            'received': received,
            'sent': sent,
            'ratio': ratio
        })
    

    完全更新的代码

    • 3

相关问题

  • 如何将 for 循环拆分为 3 个单独的数据框?

  • 如何检查 Pandas DataFrame 中的所有浮点列是否近似相等或接近

  • “load_dataset”如何工作,因为它没有检测示例文件?

  • 为什么 pandas.eval() 字符串比较返回 False

  • Python tkinter/ ttkboostrap dateentry 在只读状态下不起作用

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行?

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    何时应使用 std::inplace_vector 而不是 std::vector?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Martin Hope
    Aleksandr Dubinsky 为什么 InetAddress 上的 switch 模式匹配会失败,并出现“未涵盖所有可能的输入值”? 2024-12-23 06:56:21 +0800 CST
  • Martin Hope
    Phillip Borge 为什么这个简单而小的 Java 代码在所有 Graal JVM 上的运行速度都快 30 倍,但在任何 Oracle JVM 上却不行? 2024-12-12 20:46:46 +0800 CST
  • Martin Hope
    Oodini 具有指定基础类型但没有枚举器的“枚举类”的用途是什么? 2024-12-12 06:27:11 +0800 CST
  • Martin Hope
    sleeptightAnsiC `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它? 2024-11-09 07:18:53 +0800 CST
  • Martin Hope
    The Mad Gamer 何时应使用 std::inplace_vector 而不是 std::vector? 2024-10-29 23:01:00 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST
  • Martin Hope
    MarkB 为什么 GCC 生成有条件执行 SIMD 实现的代码? 2024-02-17 06:17:14 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve