如何在迭代它时将列表拆分为两个并使用LINQ将结果放在元组中?(How to split a list in two while iterating over it and place the res

编程入门 行业动态 更新时间:2024-10-28 11:22:55
如何在迭代它时将列表拆分为两个并使用LINQ将结果放在元组中?(How to split a list in two while iterating over it and place the results in a tuple using LINQ?)

如果我有一个可枚举的话是否可能

Input :[1,2,3,4] Output :([1,3],[2,4])

迭代一次并开始将奇数放在序列/列表/中,甚至在另一个中,最后将两个序列放在一个元组中?

我尝试过类似的东西:

class Elem { int x; } (rez1,rez2)=from value in Generator() let odds=Enumerable.Empty<Elem>() let evens=Enumerable.Empty<Elem>() select(value%2==0?odds.**append**(value):odds.**append**(value))

我如何指导元组的一个元素的选择或每个元素的另一个元素?我可以在linq查询中有两个可变列表吗?

PS我试图实现类似的东西(类似于Haskell):

select element -> predicate True -> goes to first element of result tuple predicate False-> goes to second element of result tuple

一切都在0(n)

Is it possible if i have an enumerable

Input :[1,2,3,4] Output :([1,3],[2,4])

to iterate once over it and start putting odd numbers in a sequence/list/whatever and even in another and at the end placing the two sequences in a tuple?

I have tried something like:

class Elem { int x; } (rez1,rez2)=from value in Generator() let odds=Enumerable.Empty<Elem>() let evens=Enumerable.Empty<Elem>() select(value%2==0?odds.**append**(value):odds.**append**(value))

How can i direct the select over one element of the tuple or the other for each element?Can i have two mutable lists inside a linq query?

P.S I am trying to achieve something like (similar to Haskell) :

select element -> predicate True -> goes to first element of result tuple predicate False-> goes to second element of result tuple

Everything in 0(n)

最满意答案

创建如下的元组。 对奇数和偶数值运行两个查询。

Tuple.Create(input.Where(c => c % 2 == 1).ToArray(), input.Where(c => c % 2 == 0).ToArray());

Create tuple like the following. Run two queries for odd and even values.

Tuple.Create(input.Where(c => c % 2 == 1).ToArray(), input.Where(c => c % 2 == 0).ToArray());

更多推荐

本文发布于:2023-07-20 12:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1199608.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:放在   时将   组中   两个   迭代

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!