2022.08.05 01:50 PM
I'm wondering on how to retrieve the key given the value of a dictionary?
The way to get value given the key is
array[key] - this will output the value, but what if I have the value and am looking to get the key?
2022.08.05 02:51 PM - edited 2022.08.05 02:55 PM
q)d:`a`b`c!1 2 3
q)d[`a]
1
q)d?1
`a
q)d:`a`b`c`d!1 2 3 1
q)d?1
`a
be mindful, with this method and duplicate values, only the first key is returned
2022.08.05 02:51 PM - edited 2022.08.05 02:55 PM
q)d:`a`b`c!1 2 3
q)d[`a]
1
q)d?1
`a
q)d:`a`b`c`d!1 2 3 1
q)d?1
`a
be mindful, with this method and duplicate values, only the first key is returned
2022.08.05 03:19 PM
This is exactly, what I've been looking for, thank you!
2022.08.05 08:14 PM
you also can use where clause, which return all keys that have duplicates:
q)dic:`a`b`c`d!1 2 3 1
q)where dic=1
q)`a`d
q)
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.