英文:
How do I chunk a list in KDB?
问题
我想将一个列表重新排列成大小为n的“块”:
0 1 2 3 4 5 6 7 8 9
0 1 2
1 2 3
2 3 4
在这种情况下,n=3。
在KDB中以有效的方式完成这个任务的方法是什么?
英文:
I want to rearrange a list into n-sized 'chunks':
0 1 2 3 4 5 6 7 8 9
0 1 2
1 2 3
2 3 4
where n=3 in this case.
What's an efficient way to do this in KDB that works for long lists?
答案1
得分: 2
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9
英文:
Could build a list of indices and index into your list. Not really sure what you want to do when there are less than 3/n for the cut. m below stops before nulls are populated. m2 below includes the nulls.
l:til 10;
m:{[n;l] @[l;(til n)+/: til neg[n-1]+count l]}
q)m[3;l]
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9
q)m[4;l]
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
5 6 7 8
6 7 8 9
m2:{[n;l] @[l;(til n)+/: til count l]}
q)m2[4;l]
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
5 6 7 8
6 7 8 9
7 8 9
8 9
9
</details>
# 答案2
**得分**: 1
## 滑动窗口
要实现你示例中所述的输出,可以使用修改过的版本的[滑动窗口](https://code.kx.com/q/kb/programming-idioms/#how-do-i-apply-a-function-to-a-sequence-sliding-window)函数:
q){(x-1)_{ 1_x,y }[x#0;y]}[3;til 10]
0 1 2
1 2 3
2 3 4
...
## 切割列表
要将列表切割成大小为n的块,请使用[take `#`](https://code.kx.com/q/ref/take/#atom-or-list)运算符。例如,当n=3时:
q)0N 3#til 10
0 1 2
3 4 5
6 7 8
,9
相同的符号也可用于将列表切割成n部分。例如,n=3:
q)3 0N#til 10
0 1 2
3 4 5
6 7 8 9
如文档中所述:
> x 中的空值将导致该维度变为最大值。
<details>
<summary>英文:</summary>
## Sliding window
To achieve the output set out by your example you could use a modified version of this [sliding window](https://code.kx.com/q/kb/programming-idioms/#how-do-i-apply-a-function-to-a-sequence-sliding-window) function:
q){(x-1)_{ 1_x,y }[x#0;y]}[3;til 10]
0 1 2
1 2 3
2 3 4
...
## Chunking a list
To cut a list into n sized chunks use the [take `#`](https://code.kx.com/q/ref/take/#atom-or-list) operator. For example, when n=3:
q)0N 3#til 10
0 1 2
3 4 5
6 7 8
,9
The same notation can also be used to cut a list into n parts. For example n=3:
q)3 0N#til 10
0 1 2
3 4 5
6 7 8 9
As stated in the docs:
> A null in x will cause that dimension to be maximal.
</details>
# 答案3
**得分**: 0
q)\ts:100 {flip til[x]+:til 1+y-x}[3;1000000]
2181 97555184
q)\ts:100 {(x-1)_{ 1_x,y }[x#0;y]}[3;til 1000000]
17120 89166928
q){flip til[x]+:til 1+y-x}[3;1000000]~{(x-1)_{ 1_x,y }[x#0;y]}[3;til 1000000]
1b
<details>
<summary>英文:</summary>
q)\ts:100 {flip til[x]+:til 1+y-x}[3;1000000]
2181 97555184
q)\ts:100 {(x-1)_{ 1_x,y }[x#0;y]}[3;til 1000000]
17120 89166928
q){flip til[x]+:til 1+y-x}[3;1000000]~{(x-1)_{ 1_x,y }[x#0;y]}[3;til 1000000]
1b
</details>
# 答案4
**得分**: 0
以下是您要翻译的内容:
这里有另一个更快的(尽管内存效率较低)解决方案,适用于所有类型的列表(我相信用户21344792的只适用于数字):
\ts:100 {x[] each -2_(til count x)+(count x)#enlist til y}[til 1000000;3]
8513 153166544
在这里,适用于一组符号:
q){x[] each -2_(til count x)+(count x)#enlist til y}[a
bc
de
fg
hi
j`k;3]
a b c
b c d
c d e
d e f
e f g
f g h
g h i
h i j
i j k
该解决方案首先生成一个数字矩阵,其中每行元素的值比上面的行大一,即:
0 1 2
1 2 3
2 3 4
然后,这用作我们要用来索引列表的值的列表,以生成切分。
<details>
<summary>英文:</summary>
Here's another faster (though less memory efficient) solution that is generalisable to lists of all types (I believe user21344792's only works for numbers):
\ts:100 {x[] each -2_(til count x)+(count x)#enlist til y}[til 1000000;3]
8513 153166544
Here, working for a list of syms:
q){x[] each -2_(til count x)+(count x)#enlist til y}[`a`b`c`d`e`f`g`h`i`j`k;3]
a b c
b c d
c d e
d e f
e f g
f g h
g h i
h i j
i j k
The solution works by first generating a matrix of numbers, where the value of each row element is one bigger than the row "above" i.e.
0 1 2
1 2 3
2 3 4
This then serves as the list of values we want to index our list by to producde the cut
</details>
# 答案5
**得分**: 0
每一列的结果都是前一列的`1_`。使用[Do的扫描形式](https://code.kx.com/q/ref/accumulators/#do)
```q
q)flip{(til[x]-x-1)_'(x-1)_[1;]\y}[3] til 10
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9
或者使用Drop Each Left和Drop Each
q)flip{neg[reverse tx]_'(tx:til x)_\:y}[3] til 10
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9
英文:
Each column of the result is a 1_
of the preceding column. Use the Scan form of Do
q)flip{(til[x]-x-1)_'(x-1)_[1;]\y}[3] til 10
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9
or Drop Each Left and Drop Each
q)flip{neg[reverse tx]_'(tx:til x)_\:y}[3] til 10
0 1 2
1 2 3
2 3 4
3 4 5
4 5 6
5 6 7
6 7 8
7 8 9
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论