二维数组按或列出 [英] Two dimensional array group by or to list

查看:71
本文介绍了二维数组按或列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我有以下二维数组:



Dear all,

I have the following Two Dimensional Array:

object[,] cellValues





[1,1 ] =x

[1,2] =y

[1,3] =z



[2,1] =a

[2,2] =b

[2,3] =c



[3,1] =我

[3,2] =j

[3 ,3] =k



[4,1] =p

[4,2] =q

[4,3] =r



[5,1] =m

[ 5,2] =n

[5,3] =o



现在我想改造上面的两个字典或列表中的维数:



1 [x,y,z]

2 [a,b,c]

3 [i,j,k]

4 [p,q,r]

5 [m,n,o]



如何使用LINQ和/ foreach这样做需要很长时间才能完成此操作?

请注意,Two Dimensional数组包含513360个值。



我尝试了什么:





[1,1] = "x"
[1,2] = "y"
[1,3] = "z"

[2,1] = "a"
[2,2] = "b"
[2,3] = "c"

[3,1] = "i"
[3,2] = "j"
[3,3] = "k"

[4,1] = "p"
[4,2] = "q"
[4,3] = "r"

[5,1] = "m"
[5,2] = "n"
[5,3] = "o"

Now I am trying to transform the above Two Dimensional array into Dictionary or List:

1 [x,y,z]
2 [a,b,c]
3 [i,j,k]
4 [p,q,r]
5 [m,n,o]

How to do this using LINQ as for/foreach is taking long time to complete this?
Please note that, the Two Dimensional array containf 513360 values.

What I have tried:

var xLimit = Enumerable.Range(0, cellValues.GetUpperBound(0) + 1); 
var yLimit = Enumerable.Range(0, cellValues.GetUpperBound(1) + 1);
var result = xLimit.SelectMany(x => yLimit.Select(y => cellValues[x, y]));

推荐答案

Linq可能不会加速这个:它不会删除它只是隐藏的循环并推迟它直到你需要得到它被评估的结果。



相反,看一下使用 Parallel.For [ ^ ],它将鼓励您的计算机使用多个核心。这可能会大大减少处理它所需的时间。或者由于线程设置和切换开销,它可能会使情况变得更糟。在这种情况下,将循环划分为多个部分(目标PC中每个核心一个)并在块中执行,然后在完成时组装块。
Linq probably won't speed this up: it doesn't remove the loop it just hides and defers it until you need the results at which point it is evaluated.

Instead, look at using Parallel.For[^] for your outer loop and it will "encourage" your computer to make use of multiple cores. That may significantly reduce the time taken to process it. Or it may make it worse due to the thread setup and switching overhead. In that case, divide your loop into multiple sections (one per core in your target PC) and do it in "chunks" then assemble the chunks when completed.


您可以使用AsParallel()有助于以并行方式执行行查询的方法。

You could use AsParallel() method which help to execute line query as parallel.
var xLimit = Enumerable.Range(0, cellValues.GetUpperBound(0) + 1); 
var yLimit = Enumerable.Range(0, cellValues.GetUpperBound(1) + 1);
var result = xLimit.AsParallel().SelectMany(x => yLimit.AsParallel().Select(y => cellValues[x, y]));



了解更多详情; https://msdn.microsoft.com/tr -tr / library / system.linq.parallelenumerable.asparallel(v = vs.110).aspx [ ^ ]


这里可能有用。不使用LINQ,所以它应该更快。



Here's something that might work. Doesn't use LINQ, so it should be faster.

public class Model
{
    public List<string> Values { get; set; }
}

string[,] parts = new string[,] {{"a","b","c"},{"d","e","f"}};
List<Model> list = new List<Model>();

//find out how big our array is
int dim1 = parts.GetLength(0);
int dim2 = parts.GetLength(1);

for (int i = 0; i < dim1; i++)
{
    List<string> values = new List<string>();
    for (int j = 0; j < dim2; j++)
    {
        values.Add(parts[i,j]);
    }
    list.Add(new Model(){ Values = values });
}


这篇关于二维数组按或列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆