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

如何从动态字典列表中获取最新值并提取kPassed/kFailed

从动态字典列表中提取最新monitoraction项的kPassed/kFailed值

问题背景

需要将Robot Framework测试执行中动态生成的字典列表传入Python函数,提取其中的kPassed和kFailed标识,但仅基于called_by为monitoraction的最新字典项(即timestamp值最大的项)进行操作。例如示例中timestamp=3的项为最新添加项,仅处理该项即可。

原代码示例:

list_of_dictionaries = [
    {'called_by': 'monitoraction', 'arguments': {'action': 'kPassed'},'timestamp': 1},
    {'called_by': 'monitoraction', 'arguments': {'action': 'kPassed'},'timestamp': 2},
    {'called_by': 'monitoraction', 'arguments': {'action': 'kFailed'},'timestamp': 3}]

def extract_kPassed(list_of_dictionaries: list):
    retVal = []
    for item in list_of_dictionaries:
        try:
            if item['called_by'] == 'monitoraction':
                if item['arguments'].get('action', None) == 'kPassed':
                    retVal.append(item['arguments'].get('action', None))             
                    retStr = ''.join(retVal)
        except:
            pass
    return retStr

def extract_DTC_kFailed(list_of_dictionaries: list):
    retVal = []
    for item in list_of_dictionaries:
        try:
            if item['called_by'] == 'monitoraction':
                if item['arguments'].get('action', None) == 'kFailed':
                    retVal.append(item['arguments'].get('action', None))                    
                    retStr = ''.join(retVal)
        except:
            pass
    return retStr

解决方案

核心思路

先筛选出所有called_by为monitoraction的字典项,再通过timestamp值找到最新的那一项,最后基于该项提取目标值,无需遍历整个列表。

优化后的代码实现

list_of_dictionaries = [
    {'called_by': 'monitoraction', 'arguments': {'action': 'kPassed'},'timestamp': 1},
    {'called_by': 'monitoraction', 'arguments': {'action': 'kPassed'},'timestamp': 2},
    {'called_by': 'monitoraction', 'arguments': {'action': 'kFailed'},'timestamp': 3}]

def get_latest_monitor_item(dict_list):
    # 筛选符合条件的项
    monitor_items = [item for item in dict_list if item.get('called_by') == 'monitoraction']
    if not monitor_items:
        return None
    # 按timestamp取最大值对应的项
    return max(monitor_items, key=lambda x: x.get('timestamp', 0))

def extract_latest_action(dict_list):
    latest_item = get_latest_monitor_item(dict_list)
    return latest_item['arguments'].get('action', None) if latest_item else None

# 使用示例
latest_action = extract_latest_action(list_of_dictionaries)
print(latest_action)  # 输出: kFailed

# 按需判断输出
if latest_action == 'kPassed':
    print("测试通过")
elif latest_action == 'kFailed':
    print("测试失败")

适配原有函数的版本

如果需要保留原有的两个提取函数,可以修改为:

def extract_kPassed(dict_list):
    latest_action = extract_latest_action(dict_list)
    return latest_action if latest_action == 'kPassed' else ''

def extract_DTC_kFailed(dict_list):
    latest_action = extract_latest_action(dict_list)
    return latest_action if latest_action == 'kFailed' else ''

# 使用示例
print(extract_kPassed(list_of_dictionaries))  # 输出: ''
print(extract_DTC_kFailed(list_of_dictionaries))  # 输出: 'kFailed'

代码说明

  1. get_latest_monitor_item:通过列表推导式筛选目标项,再用max()结合lambda表达式快速找到timestamp最大的项,无符合项时返回None避免报错。
  2. extract_latest_action:基于最新项提取action值,用get()方法处理键不存在的情况。
  3. 适配版函数:直接复用提取最新action的逻辑,按需返回对应值或空字符串,逻辑更简洁高效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 13:55:14