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

如何在二维列表中筛选满足双条件的元素(ResNet V2推理场景)

解决InceptionResnetV2推理结果的两个筛选需求

没问题,我帮你写两个直接可用的Python代码片段,完美搞定这两个筛选任务。首先得提个关键细节:你的分数是字符串类型,必须先转成浮点数才能正确做数值比较,不然用字符串直接比会出逻辑错误哦!

1. 筛选包含'cheese'且分数大于0.90000的元素

用列表推导式就能快速实现,遍历每个元素后同时校验两个条件:标签含'cheese'、分数转成浮点数后大于0.9。

list_total = [('cheese cake', '0.99597'), ('cheese soup', '0.00114'), ('cheese candy', '0.00102'), ('red cake', '0.00098'), ('red soup', '0.00039'), ('red candy', '0.00029'), ('blue cake', '0.00019'), ('blue soup', '0.00001'), ('blue candy', '0.00001')]

# 执行筛选
filtered_high_conf = [item for item in list_total if 'cheese' in item[0] and float(item[1]) > 0.9]

print(filtered_high_conf)
# 输出结果:[('cheese cake', '0.99597')]

2. 筛选包含'cheese'且分数最大的元素

先把所有含'cheese'的元素过滤出来,再用max()函数指定按分数的浮点数大小排序,直接拿到分数最高的那个元素。

# 先过滤出所有含cheese的条目
cheese_only_items = [item for item in list_total if 'cheese' in item[0]]

# 找到分数最高的条目
max_score_cheese = max(cheese_only_items, key=lambda x: float(x[1]))

print(max_score_cheese)
# 输出结果:('cheese cake', '0.99597')

额外提示

如果你的推理结果里可能没有含'cheese'的元素,建议加个空值判断避免报错:

if cheese_only_items:
    max_score_cheese = max(cheese_only_items, key=lambda x: float(x[1]))
else:
    max_score_cheese = None  # 或者设置你需要的默认值

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:18:09