Q 語言 - 列表



列表是q 語言的基本構建模組,因此透徹地瞭解列表非常重要。列表只是一組按順序排列的原子(原子元素)和其他列表(一個或多個原子的組)。

列表型別

通用列表用匹配的括號括住其專案,並用分號分隔它們。例如 -

(9;8;7)   or   ("a"; "b"; "c")   or   (-10.0; 3.1415e; `abcd; "r")

如果一個列表包括相同型別的原子,則稱為統一列表。否則稱為通用列表(混合型別)。

計數

我們可以透過其計數獲取列表中的專案數。

q)l1:(-10.0;3.1415e;`abcd;"r")    / Assigning variable name to general list

q)count l1                        / Calculating number of items in the list l1
4

簡單列表的示例

q)h:(1h;2h;255h)                    / Simple Integer List

q)h
1 2 255h

q)f:(123.4567;9876.543;98.7)        / Simple Floating Point List

q)f
123.4567 9876.543 98.7

q)b:(0b;1b;0b;1b;1b)                / Simple Binary Lists

q)b
01011b

q)symbols:(`Life;`Is;`Beautiful)    / Simple Symbols List

q)symbols
`Life`Is`Beautiful

q)chars:("h";"e";"l";"l";"o";" ";"w";"o";"r";"l";"d") 
                                    / Simple char lists and Strings.
q)chars
"hello world"

**注意 - 一個 char 的簡單列表稱為字串。**

一個列表包含原子或列表。要建立一個單項列表,我們使用 -

q)singleton:enlist 42

q)singleton
,42

要區分原子和等效單例,請檢查其型別的符號。

q)signum type 42
-1i

q)signum type enlist 42
1i
廣告