英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论