在KDB中如何对列表进行分块?

huangapple go评论134阅读模式
英文:

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':

  1. 0 1 2 3 4 5 6 7 8 9
  2. 0 1 2
  3. 1 2 3
  4. 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.

  1. l:til 10;
  2. m:{[n;l] @[l;(til n)+/: til neg[n-1]+count l]}
  3. q)m[3;l]
  4. 0 1 2
  5. 1 2 3
  6. 2 3 4
  7. 3 4 5
  8. 4 5 6
  9. 5 6 7
  10. 6 7 8
  11. 7 8 9
  12. q)m[4;l]
  13. 0 1 2 3
  14. 1 2 3 4
  15. 2 3 4 5
  16. 3 4 5 6
  17. 4 5 6 7
  18. 5 6 7 8
  19. 6 7 8 9
  20. m2:{[n;l] @[l;(til n)+/: til count l]}
  21. q)m2[4;l]
  22. 0 1 2 3
  23. 1 2 3 4
  24. 2 3 4 5
  25. 3 4 5 6
  26. 4 5 6 7
  27. 5 6 7 8
  28. 6 7 8 9
  29. 7 8 9
  30. 8 9
  31. 9
  32. </details>
  33. # 答案2
  34. **得分**: 1
  35. ## 滑动窗口
  36. 要实现你示例中所述的输出,可以使用修改过的版本的[滑动窗口](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
...

  1. ## 切割列表
  2. 要将列表切割成大小为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

  1. 相同的符号也可用于将列表切割成n部分。例如,n=3

q)3 0N#til 10
0 1 2
3 4 5
6 7 8 9

  1. 如文档中所述:
  2. &gt; x 中的空值将导致该维度变为最大值。
  3. <details>
  4. <summary>英文:</summary>
  5. ## Sliding window
  6. 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
...

  1. ## Chunking a list
  2. 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

  1. 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

  1. As stated in the docs:
  2. &gt; A null in x will cause that dimension to be maximal.
  3. </details>
  4. # 答案3
  5. **得分**: 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

  1. <details>
  2. <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

  1. </details>
  2. # 答案4
  3. **得分**: 0
  4. 以下是您要翻译的内容:
  5. 这里有另一个更快的(尽管内存效率较低)解决方案,适用于所有类型的列表(我相信用户21344792的只适用于数字):
  1. \ts:100 {x[] each -2_(til count x)+(count x)#enlist til y}[til 1000000;3]
  2. 8513 153166544
  1. 在这里,适用于一组符号:

q){x[] each -2_(til count x)+(count x)#enlist til y}[abcdefghij`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

  1. 该解决方案首先生成一个数字矩阵,其中每行元素的值比上面的行大一,即:

0 1 2
1 2 3
2 3 4

  1. 然后,这用作我们要用来索引列表的值的列表,以生成切分。
  2. <details>
  3. <summary>英文:</summary>
  4. Here&#39;s another faster (though less memory efficient) solution that is generalisable to lists of all types (I believe user21344792&#39;s only works for numbers):
  5. \ts:100 {x[] each -2_(til count x)+(count x)#enlist til y}[til 1000000;3]
  6. 8513 153166544
  7. Here, working for a list of syms:
  8. q){x[] each -2_(til count x)+(count x)#enlist til y}[`a`b`c`d`e`f`g`h`i`j`k;3]
  9. a b c
  10. b c d
  11. c d e
  12. d e f
  13. e f g
  14. f g h
  15. g h i
  16. h i j
  17. i j k
  18. The solution works by first generating a matrix of numbers, where the value of each row element is one bigger than the row &quot;above&quot; i.e.
  19. 0 1 2
  20. 1 2 3
  21. 2 3 4
  22. This then serves as the list of values we want to index our list by to producde the cut
  23. </details>
  24. # 答案5
  25. **得分**: 0
  26. 每一列的结果都是前一列的`1_`。使用[Do的扫描形式](https://code.kx.com/q/ref/accumulators/#do)
  27. ```q
  28. q)flip{(til[x]-x-1)_&#39;(x-1)_[1;]\y}[3] til 10
  29. 0 1 2
  30. 1 2 3
  31. 2 3 4
  32. 3 4 5
  33. 4 5 6
  34. 5 6 7
  35. 6 7 8
  36. 7 8 9

或者使用Drop Each Left和Drop Each

  1. q)flip{neg[reverse tx]_&#39;(tx:til x)_\:y}[3] til 10
  2. 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
英文:

Each column of the result is a 1_ of the preceding column. Use the Scan form of Do

  1. q)flip{(til[x]-x-1)_&#39;(x-1)_[1;]\y}[3] til 10
  2. 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

or Drop Each Left and Drop Each

  1. q)flip{neg[reverse tx]_&#39;(tx:til x)_\:y}[3] til 10
  2. 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

huangapple
  • 本文由 发表于 2023年3月7日 01:32:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654016.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定