Tutorials/Dictionaries

From Kx Wiki
Jump to: navigation, search

Prerequisites: The Basics, Datatypes, Lists, Functions.

Contents

Dictionaries

We have seen atoms and we have seen lists. A dictionary is a mapping between two lists: keys and values.

q)populations
Texas     | 26448193
Florida   | 19552860
NewYork   | 26448193
Illinois  | 12882135
California| 38332521

The syntax for creating a dictionary uses an exclamation mark (bang symbol) as follows:

q)`a`b`c!1 2 3          / <keylist>!<valuelist>
a| 1
b| 2
c| 3
q)`a`b`c!1 2            / key list and value list must be the same length
'length
q)populations:`Texas`Florida`NewYork`Illinois`California!26448193 19552860 26448193 12882135 38332521
q)type populations
99h

The type of a dictionary is always 99h.

Tip: q does not require keys to be unique, but it is highly recommended!

We can access the values of the dictionary by using the keys as indices:

q)populations[`Illinois]
12882135
q)populations`Texas`California
26448193 38332521

What if we want to go the other direction and get the keys from the values? This is identical to how we did it in Tutorials/Lists. Either use search/lookup to get the first key, or comparison to get all of them:

q)mydict:`a`b`c`d`e`f`g!1 2 3 2 1 2 4
q)mydict?2
`b
q)where mydict=2
`b`d`f

Another useful pair of functions are key and value. See what happens when you apply them to your dictionary!

From dictionary to table

The values of a dictionary can be of any type: numbers, symbols, strings, mixed lists, a list of lists... etc.

Tip: Keys can also be of any type, but you will have trouble accessing the dictionary values if the keys are not all the same type.

q) / some examples...
q)`a`b`c!"hey"
a| h
b| e
c| y
q)1 2 3!("h";2;5 6)
1| "h"
2| 2
3| 5 6
q)"abc"!(til 10;til 20;til 15)
a| 0 1 2 3 4 5 6 7 8 9
b| 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
c| 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

We are going to create a table, by first creating a dictionary whose values are lists, all of the same length.

q)mydict:`abc`def`ghi!(1 2 3 4 5 6; 7 8 9 10 11 12; 13 14 15 16 17 18)
q)mydict
abc| 1  2  3  4  5  6 
def| 7  8  9  10 11 12
ghi| 13 14 15 16 17 18

To make a table, we simply flip the dictionary!

q)mytable: flip mydict
q)mytable
abc def ghi
-----------
1   7   13 
2   8   14 
3   9   15 
4   10  16 
5   11  17 
6   12  18 

As you can see, mytable has columns with headers which correspond to the original keys.

Exercises

1. Create the populations dictionary (above). Find the ratio of the population of California to the population of Texas.

2. Find the state with the largest population. (max will return the largest value from a list or dictionary.)

3. Create the following dictionary which maps numbers to strings:

q)jobs
13| "open"
14| "closed"
15| "pending"
16| "cancelled"
17| "stuck"

4. Edit jobs so that the values are now simply the first letter of each word:

q)jobs
13| o
14| c
15| p
16| c
17| s

5. Create the following dictionary which has only one key-value pair.

q)d
a| "test"

Solutions

Personal tools
Namespaces
Variants
Actions
Navigation
Print/export
Toolbox