1. Dictionary Basis
1. definition
The dictionary is key-values pairs, but in q, the dictionary is stored by list. Dictionary creation uses operators!, read bang:) All dictionary types are 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
You can use the operators key, value, count to get the keys, values, and number of dictionaries, respectively.
Although the q language does not enforce the historical mistake of keys, it provides a unique output for each input value, and only the first key will be seen.
When you know that the key of a dictionary is unique, you can use the command `u'to convert the dictionary into a hash table, which will speed up the search compared with the original linear search.
q)(`u#`a`b`c)!10 20 30
Note: Unlike traditional languages, dictionaries in different order are different in q.
q)(`a`b`c!10 20 30)~`a`c`b!10 30 20 0b
2. Empty and Singular Dictionaries
Empty dictionary:
()!()
Empty dictionary with type:
`symbol$()!`float$()
In a singleton dictionary, you must use the enlist form to form a list, otherwise you will make an error.
q)(enlist `x)!enlist 42 x| 42
3. search
Use parentheses or juxtapositions:
q)d:`a`b`c!10 20 30 q)d[`a] 10 q)d `b 20
When the lookup value is not in the key table of the dictionary, the Null value of the initial value type in the list of values is returned:
q)d[`x] 0N
4. Reverse lookup?
Similar to lists, the return value corresponds to the key in the dictionary.
q)d:`a`b`c`a!10 20 30 10 q)d?10 `a
When the value found is not in the dictionary's list of values, the Null value of the initial value type in the key list is returned:
q)d:`a`b`c`a!10 20 30 10 q)d?40 `
5. Dictionaries and lists
A sparse list can be represented by a dictionary:
q)d1:0 100 500000!10 20 30 q)d2:0 99 1000000!100 200 300 q)d1+d2 0 | 110 100 | 20 500000 | 30 99 | 200 1000000| 300
6. Not unique keys and values
As mentioned earlier, when the key is not unique, it returns the value when the first key appears:
q)ddup:`a`b`a`c!10 20 30 20 q)ddup[`a] 10
Reverse Search Similarly:
q)ddup?30 `a q)ddup?20 `b
7. Non-simple keys and values
Key and value are nested lists:
q)d:(`a`b; `c`d`e; enlist `f)!10 20 30 q)d `f 30 q)d?20 `c`d`e q)d:`a`b`c!(10 20; 30 40 50; enlist 60) q)d `b 30 40 50 q)d?30 40 50 `b q)d?enlist 60
Note that the key or value of a single element should be separately generated into a list (enlist), otherwise there will be the following problems:
q)dwhackey:(1 2; 3 4 5; 6; 7 8)!10 20 30 40 / atom 6 is whack q)dwhackey 1 2 10 q)dwhackey 6 0N q)dwhackval:10 20 30 40!(1 2; 3 4 5; 6; 7 8) / atom 6 is whack q)dwhackval?3 4 5 20 q)dwhackval?6 0N
This will cause the search to fail and return the Null value.
2. Dictionary operation
1. Amend and Upsert
update:
q)d:`a`b`c!10 20 30 q)d[`b]:42
insert:
q)d:`a`b`c!10 20 30 q)d[`x]:42
In q language, update/insert operation is called upsert operation.
2. Extracting Subdictionaries
Using the extraction operator #, the left operator is the key of the sub-dictionary and the right operator is the original dictionary. In addition, when the original dictionary has duplicate keys, only the first keys are extracted:
q)ddup:`a`b`a`c!10 20 30 20 q)`a`c#ddup a| 10 c| 20
3. delete
Using the operator, the usage is similar to, but it needs to be noted that space is added before and after.
q)d:`a`b`c!10 20 30 q)`a`c _ d b| 20 q)(enlist `b) _ d
Deleting all key-value pairs yields an empty dictionary with a type:
q)d:`a`b`c!10 20 30 q)`a`b`c _ d q)-3!`a`b`c _ d "(`symbol$())!`long$()"
The operator cut works the same as in the dictionary.
Another rarely used use is that the dictionary is on the left side of and the right operand is a single key, indicating that the dictionary deletes the key:
q)d _ `b a| 10 c| 30
4. Basic operations on dictionaries
Some common basic operations are shown in the following examples, which are very easy to understand.
q)d:`a`b`c!10 20 30 q)neg d a| -10 b| -20 c| -30
Dictionary addition: The same key value is added, while different key values are retained.
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
5. join,
Use, merge dictionary operation, because q language is right to left characteristics, so if the two dictionaries have the same key value, then the right will be retained.
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
Therefore, the order of dictionary merging is very important.
6. Coalesce ^
This method of merging is similar to merging, but different from it, when the value of ^ right-hand item is Null, the left-hand item is not covered. See the following example:
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 q)d1,d2 a| 10 b| 200 c| d| 400
7. Arithmetic and Equal Operators
For equality comparison, because null represents missing values, all null values are considered equal.
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!0N 20 30)=`b`c`d!20 300 0N a| 1 b| 1 c| 0 d| 1 q)(`a`b`c!10 20 30)<`b`c`d!20 300 400 a| 0 b| 0 c| 1 d| 1
3. column dictionary
Column Dictionary is the basis of table.
1. Definitions and terminology
A general column dictionary has the following forms:
c1...cn!(v1;...;vn)
Where ci is a symbol ic type and vi is a list of the same length. Usually vi is a simple list.
2. Simple examples
q)travelers:`name`iq!(`Dent`Beeblebrox`Prefect;42 98 126) q)travelers name| Dent Beeblebrox Prefect iq | 42 98 126
Indexes:
q)travelers[`name; 1] `Beeblebrox q)travelers[`iq; 2] 126
A single-column dictionary:
q)dc1:(enlist `c)!enlist 10 20 30 q)dc1 c| 10 20 30
4. Inversion of Column Dictionary
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
Indexes:
q)dc[`c1; 0] `a q)dc[`c1; 1] `b q)dc[`c1; 2] `c q)t[0; `c1] `a q)t[1; `c1] `b q)t[2; `c1] `c
Unlike the case of transposing rectangular lists, transposing a column dictionary does not physically re-arrange data. (Flipping does not change the storage of data)