-
Notifications
You must be signed in to change notification settings - Fork 0
List
Ferenc Bodon edited this page Jul 23, 2018
·
31 revisions
| command | Q | Python | Q example | Python example | comment | ||
|---|---|---|---|---|---|---|---|
| creating general list |
( ) with separator ; |
[ ] with separator , |
l: (3; 1.0; "hello") |
l = [3, 1.0, "hello"] |
|||
| π | creating homogenious list | whitespace is the separator | li: 3 1 2 |
li = [3, 1, 2] |
Q saves you some typing | ||
| π | list of a single item | keyword enlist
|
no special treatment | enlist 3 |
[3] |
Need of using enlist is confusing |
|
| list of pairs from two lists |
, '
|
zip |
l1 ,' l2 |
zip(l1, l2) |
|||
| π | list of generated ints |
til |
range and list |
til 3 |
list(range(3)) |
||
| π | list of generated ints between | not supported, workaround exists | 3 + til 7-3 |
list(range(3,7)) |
|||
| π | list of generated ints with steps | workaround exists | 4 + 2 * til (10 - 4) div 2 |
list(range(4,10, 2)) |
| command | Q | Python | Q example | Python example | comment | ||
|---|---|---|---|---|---|---|---|
| append | ,: |
append |
l,: 3 |
l.append(3) |
|||
| π | extend | ,: |
extend |
l1,: l2 |
l1.extend(l2) |
Q implements polymorphism better | |
| π | insert into middle | not supported, workaround exists | insert |
(i#l), x, i_l |
l.insert(i, x) |
||
| delete by index | # |
del |
l: (3#l), 4_l |
del l[3] |
|||
| delete all | # |
clear |
l: 0#l |
l.clear() |
|||
| π | pop last element | not supported, workaround exists | pop |
l.pop() |
|||
| π | remove value | : and except |
remove |
l: l except 3 |
l.remove(3) |
||
| π | remove multiple values | l1 except l2 |
[e for e in l1 if e not in l2] |
| command | Q | Python | Q example | Python example | comment | ||
|---|---|---|---|---|---|---|---|
| find first | ? |
index |
l ? 3 |
l.index(3) |
|||
| π | find multiple | ? |
not supported | l ? 3 7 |
|||
| π | find all occurrences |
= where
|
where 3 = l |
[i for i, x in enumerate(l) if x == "3] |
|||
| π | position of all occurrences of elements | group |
not supported | group 1 2 1 3 3 |
|||
| π | unique values |
distinct |
manually convert to set | distinct l |
list(set(l)) |
||
| π | intersection | inter |
l1 inter l2 |
list(set(l1) & set(l2)) or set(l1).intersection(l2)
|
|||
| π | union | union |
l1 union l2 |
list(set(l1) | set(l2)) or set(l1).union(l2)
|
|||
| π | first/last element as monadic function |
first/last
|
not supported | first l |
|||
| first/last n rows | # |
[:] |
15#l -3#l
|
l[:15] l[-3:]
|
|||
| ππ | indexing by list | same as index by number | workaround with for loop | l[0 3 9] | [l[i] for i in [0, 3, 9]]] | Q is more elegant and requires less typing. Class array in Numpy also supports indexing by list |
| command | Q | Python | Q example | Python example | comment | ||||
|---|---|---|---|---|---|---|---|---|---|
| concatenation | , |
+ |
l1, l2 |
l1 + l2 |
|||||
| equivalence | ~ |
== |
l1 ~ l2 |
l1 == l2 |
|||||
| empty list check | count |
not |
0 = count l |
not l |
|||||
| lexicographical comparison |
not first iasc
|
<= |
not first iasc (l1; l2) |
l1 < l2 |
In Q operators < and * is reserved for elementwise comparison |
||||
| list repetition |
raze enlist
|
* |
raze 3#enlist l |
3*l |
|||||
| ππ | elementwise operators | by design | not suported | ||||||
| sorting |
asc |
sorted |
asc l |
sorted(l) |
inplace sorting in only supported in Python by function sort
|
||||
| π | desc |
desc l |
sorted(l, reverse=True) |
||||||
| π | overwrite null only | ^ |
not supported | (1 2) ^ 0N 4 |