How to get range of elements in a list in KDB?

泪湿孤枕 提交于 2019-12-11 04:06:27

问题


For example, I have this list:

test:(8;12;15;19;10)

How may I select elements 2 to 4?

When I try list[2;4] it doesn't work for me.


回答1:


indexing a list is by far the fastest way.

q)a
8 1 9 5 4 6 6 1 8 5 4 9 2 7 0 1 9 2 1 8 8 1 7 2 4 5 4 2 7 8 5 6 4 1 3 3 7 8 2..
q)\t do[100000;2 3 sublist a]
109
q)\t do[100000;a 2 3 4]
15

So just follow your list with a list of indexes. BTW you can create indexes with til

q)til 2
0 1
q)2+til 2
2 3



回答2:


You can use sublist for this

test:(8;12;15;19;10);
2 3 sublist test 

This will return three elements from the list starting at index 2.




回答3:


As answered by Manish, til is the best bet here. You can define a simple function range using til to give the index range :

q)test:(8;12;15;19;10)

q)range:{x+til 1+abs[y-x]}   //include the start and end index 
q)test range[2;4] 
15j, 19j, 10j


来源:https://stackoverflow.com/questions/28525768/how-to-get-range-of-elements-in-a-list-in-kdb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!