Overview
A dictionary is a mapping defined by an explicit association between a key list and value list. The two lists must have the same count and the key list should be a unique collection.
Dictionary Basics
A dictionary is an association between a list of keys and a list of values.
All dictionaries have type 99h
q)10 20 30!1.1 2.2 3.3
10| 1.1
20| 2.2
30| 3.3
q)`a`b`c!100 200 300
a| 100
b| 200
c| 300
q)d:`a`b`c!100 200 300
q)key d
`a`b`c
q)value d
100 200 300
q)count d
3
When you know that the keys are unique you can apply the `u# attribute to the keys.
q)(`u#`a`b`c)!10 20 30
a| 10
b| 20
c| 30
Empty and Singleton Dictionaries
q)()!()
::
You can create a typed empty dictionary using typed empty key and value lists.
q)(`symbol$())!`float$()
::
Because both the keys and values are required to be lists, you must enlist atoms for a singleton dictionary.
Lookup
q)d:`a`b`c!10 20 30
q)d[`a]
10
q)d[`a`c]
10 30
Reverse Lookup with Find ?
That is, ? on a dictionary performs reverse lookup by mapping a value item to the first key associated to it.
q)d:`a`b`c`a!10 20 30 10
q)d?10
`a
Dictionary vs. List
It is possible to create a dictionary that seemingly behaves very much like an equivalent list by making the positional retrieval explicit.
Non-unique Keys and Values
As mentioned previously, non-unique keys are tolerated but lookup only sees the first occurrence.
q)ddup:`a`b`a`c!10 20 30 20
q)ddup[`a]
10
You can find all keys mapping to a given value as follows.
q)where 10=d
`a`d
Non-simple Keys and Values
q)d:(`a`b; `c`d`e; enlist `f)!10 20 30
q)d `f
30
q)d?20
`c`d`e
Operations on Dictionaries
Amend and Upsert
q)d:`a`b`c!10 20 30
q)d[`b]:42
q)d
a| 10
b| 42
c| 30
In contrast to lists, dictionaries can be extended via assignment.
q)d:`a`b`c!10 20 30
q)d[`x]:42
q)d
a| 10
b| 42
c| 30
x| 42
Extracting a Sub-Dictionary
Dictionary lookup on a key or a list of keys returns the associated values. It is possible to extract both keys and values using an overload of the Take operator #.
q)`a`c#d
a| 10
c| 30
q)(enlist `c)#d
c| 30
In the event of duplicate keys, only the first is extracted.
Removing Entries
q)d:`a`b`c!10 20 30
q)`a`c _ d
b| 20
Observe that all occurrences of a key are removed and that attempting to remove a key that does not exist has no effect.
q)`x`a _ d
b| 20
c| 30
Removing all the entries in a dictionary leaves a dictionary with empty key and value lists of the appropriate types.
q).Q.s1 `a`b`c _ d
"(`symbol$())!`long$()"
The binary keyword cut is the same as this overload of _ on a dictionary.
q)`a`c cut d
b| 20
Basic Operations on Dictionaries
q)d:`a`b`c!10 20 30
q)neg d
a| -10
b| -20
c| -30
q)d=20
a| 0
b| 1
c| 0
q)d1:`a`b`c!1 2 3
q)d2:`b`c`d!20 30 40
q)d1+d2
a| 1
b| 22
c| 33
d| 40
Join ,
q)d1:`a`b`c!10 20 30
q)d2:`x`y!40 50
q)d1,d2
a| 10
b| 20
c| 30
x| 40
y| 50
On common keys, since q is right-to-left, the value of the right operand prevails over that of the left.
q)d1:`a`b`c!10 20 30
q)d2:`a`b`c!100 200 300
q)d1,d2
a| 100
b| 200
c| 300
Observe that Join is not commutative: order matters.
q)d1:`a`b`c!10 20 30
q)d2:`c`d!300 400
q)d1,d2
a| 10
b| 20
c| 300
d| 400
q)d2,d1
c| 30
d| 400
a| 10
b| 20
Coalesce ^
The Coalesce operator is related to , in that it employs upsert semantics to merge two dictionaries. The difference is that right values prevail over left except for nulls in the right.
q)d1:`a`b`c!10 0N 30
q)d2:`b`c`d!200 0N 400
q)d1^d2
a| 10
b| 200
c| 30
d| 400
Arithmetic And Relational Operations
Add
q)d1:`a`b`c!10 20 30
q)d2:`b`c`d!200 300 400
q)d1+d2
a| 10
b| 220
c| 330
d| 400
Equality
q)(`a`b`c!10 20 30)=`b`c`d!20 300 400
a| 0
b| 1
c| 0
d| 0
q)(`a`b`c!10 20 30)<`b`c`d!20 300 400
a| 0
b| 0
c| 1
d| 1
Column Dictionaries
Column dictionaries are the foundation for tables.
Definition and Terminology
q)traveller: `c1`c2!(`a`b`c; 10 20 30)
c1| a b c
c2| 10 20 30
q)traveller[`c1][1]
`b
q)traveller[`c1][1]
`b
q)traveller[;2]
c1| `c
c2| 30
Column Dictionary with a Single Column
q)dc1:(enlist `c)!enlist 10 20 30
q)dc1
c| 10 20 30
Flipping a Column Dictionary
q)L:(10 20 30; 100 200 300)
q)L
10 20 30
100 200 300
q)flip L
10 100
20 200
30 300
A side effect of transposing the nested list data is that it reverses the slots for indexing at depth.
q)dc:`c1`c2!(`a`b`c; 10 20 30)
q)dc
c1| a b c
c2| 10 20 30
q)t:flip dc
q)t
c1 c2
-----
a 10
b 20
c 30
q)dc~flip flip dc
1b
We restate this as:
- The flip of a transposed column dictionary is a (the original!) column dictionary
Unlike the case of transposing rectangular lists, transposing a column dictionary does not physically re-arrange data.
A transposed column dictionary is stored in column order.
作者:Jeffry A. Borror