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

Groovy列表解析求助:将LUT转换为分支对应名称-镜像名映射

问题:将LUT列表转换为指定格式的分支映射

原始LUT数据

LUT = [
    [branch: "test", name: 'a', image_name: 'abc'],
    [branch: "test", name: 'b', image_name: 'abc'],
    [branch: "test", name: 'c', image_name: 'abc'],
    [branch: "test-1", name: 'd', image_name: 'abc'],
    [branch: "test-1", name: 'e', image_name: 'abc'],
    [branch: "test-2", name: 'f', image_name: 'abc'],
    [branch: "test-2", name: 'g', image_name: 'abc'],
    [branch: "test-2", name: 'h', image_name: 'abc'],
    [branch: "test-3", name: 'i', image_name: 'abc'],
    [branch: "test-3", name: 'j', image_name: 'abc'],
    [branch: "test-4", name: 'k', image_name: 'abc'],
    [branch: "test-5", name: 'l', image_name: 'abc'],
]

当前实现代码

result = [:]

for (map in LUT)
{
    if (!result.containsKey(map['branch']))
    {
        println map['name'] // prints the unique name
        result.put(map['branch'], map['name'])
    }
}

当前输出结果

[test:a, test-1:d, test-2:f, test-3:i, test-4:k, test-5:l]

期望输出格式

[test:[a:abc], test-1:[d:abc], test-2:[f:abc], test-3:[i:abc], test-4:[k:abc], test-5:[l:abc]]

解决方案

修改代码中存入result的值,将原本的字符串map['name']替换为嵌套Map结构,以name为键、image_name为值:

result = [:]

for (map in LUT) {
    def branch = map['branch']
    if (!result.containsKey(branch)) {
        // 构建嵌套Map,存储name与image_name的对应关系
        result.put(branch, [(map['name']): map['image_name']])
    }
}

说明

原代码仅将分支名直接映射到单个字符串name,现在调整为映射到新的Map对象,该对象包含name:image_name的键值对,完全匹配期望结构。同时保留了原逻辑中「仅保留每个分支第一个出现条目」的行为,最终输出与预期一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 23:01:07