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

如何将指定kdb字典优雅转换为目标列表?

Kdb 字典索引映射实现方案

给定Kdb字典:

`a`b`c!((0;3); (1;4); enlist 2)

需要生成如下格式的结果列表:

`a (index 0)
`b (index 1)
`c (index 2)
`a (index 3)
`b (index 4)

实现方法1:结合each与raze展开键值对

先将字典转换为键值对列表,再为每个键匹配其对应的索引值,最后展平结果:

q)d:`a`b`c!((0;3); (1;4); enlist 2);
q)raze {x,'enlist each y} each flip `key`value!d
`a 0
`b 1
`c 2
`a 3
`b 4

如果需要生成指定的字符串格式,添加字符串格式化逻辑:

q)raze {string[x]," (index ",string[y],")"} each flip `key`value!d
"`a (index 0)"
"`b (index 1)"
"`c (index 2)"
"`a (index 3)"
"`b (index 4)"

实现方法2:用ungroup快速展开(更简洁)

ungroup是Kdb中处理多值映射的原生函数,能直接将字典的键值对展开为结构化表格,再按需格式化:

q)d:`a`b`c!((0;3); (1;4); enlist 2);
q)res:ungroup flip d;
q)res
key value
---------
a   0
b   1
c   2
a   3
b   4

生成目标字符串格式:

q)raze string[res[`key]]," (index ",string[res[`value]],")"
"`a (index 0)"
"`b (index 1)"
"`c (index 2)"
"`a (index 3)"
"`b (index 4)"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 11:42:35