在q语言(kdb+)中,获取字典中给定键的所有值的方法是:

huangapple go评论102阅读模式
英文:

How to get all the values for a given key of a dictionary in q lang(kdb+)

问题

让我们假设我们有一个字典

ddup:`a`b`a`c!10 20 30 20

当我输入命令 ddup[a]`
输出:10
但如何输入以获得
输出:10 30

(key ddup)[where (key ddup) = `a]
`a`a
ddup[(key ddup)[where (key ddup) = `a]]
10 30
英文:

Let's say we have a dictionary

ddup:`a`b`a`c!10 20 30 20

When I write command as ddup[`a]
output: 10
But how to put input to get
output: 10 30

(key ddup)[where (key ddup) = `a]
`a`a
ddup[(key ddup)[where (key ddup) = `a]]
10 10

答案1

得分: 1

你可以通过以下方式获得你想要的结果:

(value ddup) where `a = key ddup

然而,最好的选择是一开始就不要这样设置你的字典。保持一组不同的键将更加高效。如果可能的话,我会考虑重新构建字典,像这样:

q)dict:`a`b`c!(10 30;20;20)
q)dict
a| 10 30
b| 20
c| 20
英文:

You can get the result you're looking for by doing:

(value ddup) where `a = key ddup

The best option though is to not set up your dictionary like this in the first place. Keeping to a set of distinct keys would be far more efficient.
If possible I would consider restructuring the dictionary to be like:

q)dict:`a`b`c!(10 30;20;20)
q)dict
a| 10 30
b| 20
c| 20

答案2

得分: 1

根据Sean的说法,您应该重新构造您的数据,而不是绕过它。正如文档中提到的,对具有重复键的字典和表的操作是未定义的 - 这可能会在后续引发更大的问题。

https://code.kx.com/q/basics/dictsandtables/#construction

英文:

As Sean said, you should restructure your data rather than work around it. As mentioned in the documents, operations on dictionaries and tables with duplicate keys are undefined - this can cause you larger issues further down the line.

https://code.kx.com/q/basics/dictsandtables/#construction

huangapple
  • 本文由 发表于 2023年7月10日 19:05:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定