Collapsing Dictionaries

Question: A dictionary is a data structure that stores key-value information. There should always be a one-to-one relationship between each key and value, so every key should only have one value. The language, however, permits there to be a one-to-many relationship between each key and value, meaning a key can have multiple values. When accessing a key, q always picks the first value associated with it. For the dictionary '`a`a!1 2', accessing the key 'a' will yield '1'. To account for this kind of scenario, we can remove duplicate keys by appending their values into a single list, thus making the key-value relationship one-to-one.

Define a function 'collapseDict' which takes in a dictionary and returns a dictionary with non-duplicate keys, where values are grouped together by key.

Example

                                
                                q)collapseDict `a`a`b`b`c!1 2 2 2 3
a| 1 2
b| 2 2
c| ,3
q)collapseDict `a`b`c!1 2 3
a| 1
b| 2
c| 3

// Now we can do aggregations
q)sum each `a`a`b`b`c!1 2 2 2 3 // before
a| 1
a| 2
b| 2
b| 2
c| 3
q)sum each collapseDict `a`a`b`b`c!1 2 2 2 3 // after
a| 3
b| 4
c| 3
                                
                            

Solution

Tags:
dictionaries functions
Searchable Tags
algorithms api csv dictionaries feedhandler finance functions ingestion ipc iterators math optimizations realtime sql streaming strings tables temporal websockets

Email sent!

Email not sent