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

如何在Polars中从嵌套列表提取Top K元素?(RGB场景优化)

问题描述

现有数据集包含两列:图像的[R, G, B]值嵌套列表,以及经K-means算法处理后的同结构列表。需统计Top N值,目前已实现通过展开列表调用多函数的代码方案,但希望找到更简洁的实现方式,且结果需保持单行(因需处理多张图片,不可重塑数据集)。

数据集示例

shape: (1, 3)
┌───────────────────────┬───────────────────────────────────┬───────────────────────────────────┐
│ filepath              ┆ og                                ┆ seg                               │
│ ---                   ┆ ---                               ┆ ---                               │
│ str                   ┆ list[list[u8]]                    ┆ list[list[f64]]                   │
╞═══════════════════════╪═══════════════════════════════════╪═══════════════════════════════════╡
│ Viceroy_Butterfly.jpg ┆ [[86, 103, 61], [87, 102, 61], …… ┆ [[120.626352, 123.41985, 59.6664… │
└───────────────────────┴───────────────────────────────────┴───────────────────────────────────┘

现有实现代码

out.select(pl.col.og.explode()).group_by(pl.col.og).len().top_k(
    by=pl.col.len, k=10
).to_struct("result").struct.field("og").implode()

代码输出结果

shape: (1,)
og
list[list[u8]]
[[0, 0, 0], [14, 0, 0], … [201, 233, 150]]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 13:27:03