在表上使用迭代器

发布于 2025-01-19 05:24:20 字数 461 浏览 1 评论 0原文

我有这个表:

A:2.34889 2.484112 1.045939 3.359097 1.642348 1.298948 3.046995 4.077684 
B:3.845017 3.762336 3.287893 3.338063 5.861462 5.401914 3.537128 5.27197
t:([] AA:A;BB:B)

-1 + prd select (-1#AA)%(1#AA) from t 
-1 + prd select (-1#BB)%(1#BB) from t 

我想知道如何将

AA| 0.7360047
BB| 0.3711175

最后两行修改为迭代 AA 和 BB 的单行?例如,如果我有 10 个符号,我只需编写一行即可输出 10 个结果。

另外对问题标题表示歉意,我不确定如何很好地表达它,但如果需要的话很乐意进行编辑。

I have this table:

A:2.34889 2.484112 1.045939 3.359097 1.642348 1.298948 3.046995 4.077684 
B:3.845017 3.762336 3.287893 3.338063 5.861462 5.401914 3.537128 5.27197
t:([] AA:A;BB:B)

-1 + prd select (-1#AA)%(1#AA) from t 
-1 + prd select (-1#BB)%(1#BB) from t 

which outputs

AA| 0.7360047
BB| 0.3711175

I was wondering how I can modify the last two lines into a single line that iterates over AA and BB? For example, if I had 10 symbols, I would only have to write a single line to output the 10 results.

Also apologies on the question title, I am not sure how to phrase it well but am happy to edit if required.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朦胧时间 2025-01-26 05:24:20

桌子上的迭代器可以行升起(由0n!下方证明):

0N!/: t
`AA`BB!2.34889 3.845017
`AA`BB!2.484112 3.762336
`AA`BB!1.045939 3.287893
`AA`BB!3.359097 3.338063
`AA`BB!1.642348 5.861462
`AA`BB!1.298948 5.401914
`AA`BB!3.046995 3.537128
`AA`BB!4.077684 5.27197

或flip列:

0N!/: flip t
2.34889 2.484112 1.045939 3.359097 1.642348 1.298948 3.046995 4.077684
3.845017 3.762336 3.287893 3.338063 5.861462 5.401914 3.537128 5.27197

对于这种情况,您可以执行后者并将您的函数应用于每个迭代器的所有列:

{-1+prd last[x]%first x} each flip t
AA| 0.7360047
BB| 0.3711175

使用##或选择以获取要应用该功能的列的子集,如果需要:

{-1+prd last[x]%first x} each flip `AA`BB#t
AA| 0.7360047
BB| 0.3711175

更一般而言,当试图构建类似的代码以应用于列列表功能表单时,可以很有用要注意: https://code.kx.com/q/q/basics/funs/funsql/

parse "exec AA:{-1+prd last[x]%first x} AA from t"
?
`t
()
()
(,`AA)!,({-1+prd last[x]%first x};`AA)

// or cls:cols t
cls:`AA`BB ;

?[t;();();cls!({-1+prd last[x]%first x}),/:cls]
AA| 0.7360047
BB| 0.3711175

Iterators on tables can either be row rise (demonstrated by 0N! below):

0N!/: t
`AA`BB!2.34889 3.845017
`AA`BB!2.484112 3.762336
`AA`BB!1.045939 3.287893
`AA`BB!3.359097 3.338063
`AA`BB!1.642348 5.861462
`AA`BB!1.298948 5.401914
`AA`BB!3.046995 3.537128
`AA`BB!4.077684 5.27197

Or column wise with flip:

0N!/: flip t
2.34889 2.484112 1.045939 3.359097 1.642348 1.298948 3.046995 4.077684
3.845017 3.762336 3.287893 3.338063 5.861462 5.401914 3.537128 5.27197

For this case, you could do the latter and apply your function to all columns with the each iterator:

{-1+prd last[x]%first x} each flip t
AA| 0.7360047
BB| 0.3711175

Use # or select to get the subset of columns you want to apply the function to if needs be:

{-1+prd last[x]%first x} each flip `AA`BB#t
AA| 0.7360047
BB| 0.3711175

More generally, when trying to build up similar code to apply to a list of columns functional form can be useful to be aware of: https://code.kx.com/q/basics/funsql/

parse "exec AA:{-1+prd last[x]%first x} AA from t"
?
`t
()
()
(,`AA)!,({-1+prd last[x]%first x};`AA)

// or cls:cols t
cls:`AA`BB ;

?[t;();();cls!({-1+prd last[x]%first x}),/:cls]
AA| 0.7360047
BB| 0.3711175
爱格式化 2025-01-26 05:24:20

马茨的答案是一个更好、更通用的答案,但在您的特定示例中,逻辑可以很简单:

q)-1+last[t]%first t
AA| 0.7360047
BB| 0.3711175

Matts answer is a better and more general answer but in your particular example the logic can be as simple as:

q)-1+last[t]%first t
AA| 0.7360047
BB| 0.3711175
勿忘心安 2025-01-26 05:24:20

无需迭代列。

这里的迭代器的最佳用途是每个人都左将同时应用第一个最后将其应用于t

q)(last;first)@\:t
AA       BB
-----------------
4.077684 5.27197
2.34889  3.845017

该表是2列列表,因此您可以应用鸿沟。

q)-1+(%).(last;first)@\:t
AA| 0.7360047
BB| 0.3711175

为了重复使用定义此功能,它是三个联合国的组成,此处是为了清晰:

q)f:-1+ (%). (last;first)@\:
q)f t
AA| 0.7360047
BB| 0.3711175

适用于任何数量的列。

No need to iterate through the columns.

The best use of iterators here is Each Left to apply both first and last to t.

q)(last;first)@\:t
AA       BB
-----------------
4.077684 5.27197
2.34889  3.845017

That table is a 2-list, so you can apply Divide.

q)-1+(%).(last;first)@\:t
AA| 0.7360047
BB| 0.3711175

To define this for re-use, it’s a composition of three unaries, here spaced for clarity:

q)f:-1+ (%). (last;first)@\:
q)f t
AA| 0.7360047
BB| 0.3711175

Works for any number of columns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文