gpt4 book ai didi

arrays - 如何在 Qore 中将长列表分成几部分

转载 作者:行者123 更新时间:2023-12-04 18:02:26 25 4
gpt4 key购买 nike

我想像这样在 Qore 中拆分列表:

list a = (1,2,3,4,5,6);
list pieces = split_list_into_pieces(a, 2);
printf("%y\n", pieces);

期望的输出:
[[1,2], [3,4], [5,6]]

IE。我想获取一个(据说很长)的列表并将其拆分为给定(最大)长度的部分。

我可以这样做:
list sub split_list_into_pieces(list a, int length)
{
int i = 0;
list ret = ();
list temp = ();
foreach any x in (a)
{
temp += x;
i++;
if (i == length)
{
push ret, temp;
temp = ();
i = 0;
}
}
if (temp)
{
push ret, temp;
}
return ret;
}

但它不是很优雅,是吗?

有什么更好的解决办法吗?

最佳答案

你可以这样做:

list sub list_chunk(list a, int length) {
list result = ();
while (a)
push (result, extract (a, 0, length));
return result;
}

关于arrays - 如何在 Qore 中将长列表分成几部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42135370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com