count, mcount¶

Count the items of a list or dictionary

count¶

Number of items

count x     count[x]

Where x is

• a list, returns the number of its items
• a dictionary, the number of items in its value
• anything else, 1
q)count 0                            / atom
1
q)count "zero"                       / vector
4
q)count (2;3 5;"eight")              / mixed list
3
q)count each (2;3 5;"eight")
1 2 5
q)count abc!2 3 5                 / dictionary
3
q)/ the items of a table are its rows
q)count ([]city:LondonParisBerlin; country:EnglandFranceGermany)
3
q)count each ([]city:LondonParisBerlin; country:EnglandFranceGermany)
2 2 2

q)count ({x+y})
1
q)count (+/)
1

Use with each to count the number of items at each level of a list or dictionary.

q)RaggedArray:(1 2 3;4 5;6 7 8 9;0)
q)count RaggedArray
4
q)count each RaggedArray
3 2 4 1
q)RaggedDict:abc!(1 2;3 4 5;"hello")
q)count RaggedDict
3
q)count each RaggedDict
a| 2
b| 3
c| 5
q)\l sp.q
q)count sp
12

mcount¶

Moving counts

x mcount y     mcount[x;y]

Where

• x is a positive int atom
• y is a numeric list

returns the x-item moving counts of the non-null items of y. The first x items of the result are the counts so far, and thereafter the result is the moving count.

q)3 mcount 0 1 2 3 4 5
1 2 3 3 3 3
q)3 mcount 0N 1 2 3 0N 5
0 1 2 3 2 2

mcount is a uniform function.

Implicit iteration¶

mcount applies to dictionaries and tables.

q)k:k xkey update k:abcdefghi from t:flip d:ab!(10 21 3;4 5 6)

q)2 mcount d
a| 1 1 1
b| 2 2 2

q)2 mcount t
a b
---
1 1
2 2
2 2

q)2 mcount k
k  | a b
---| ---
abc| 1 1
def| 2 2
ghi| 2 2