Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Union of list of dictionaries in KDB

I am trying to join an arbitrarily long list of dictionaries into one dictionary where matching keys will take the union of distinct values and new keys are simply inserted. Is there a way to do that using uj?

dd1:(`a`b`c)!(1 2; 3 4; 5 6);
dd2:(`b`c`d)!(7 8; 9 10; 0 1);
dd3:(`a`f`e)!(3 4; 1 1; 2 2);

(uj)/:(dd1;dd2;dd3) just returns the last dict.

What I want is:

(`a`b`c`d`e`f)!(1 2 3 4; 3 4 7 8; 5 6 9 10; 0 1; 2 2; 1 1)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

One method not using uj

q)(,'/)(dd1;dd2;dd3)
a| 1 2 3 4
b| 3 4 7 8
c| 5 6 9 10
d| 0 1
f| 1 1
e| 2 2

If the order of the keys is important

q)`a`b`c`d`e xcols(,'/)(dd1;dd2;dd3)
a| 1 2 3 4
b| 3 4 7 8
c| 5 6 9 10
d| 0 1
e| 2 2
f| 1 1

To ensure only distinct values are taken

q)distinct each(,'/)(dd1;dd2;dd3)
a| 1 2 3 4
b| 3 4 7 8
c| 5 6 9 10
d| 0 1
f| ,1
e| ,2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading