c#筛选双精度[]值的列表

本文关键字:列表 筛选 双精度 | 更新日期: 2023-09-27 18:03:33

问题描述:

我有一个双精度[]值的列表,表示向量,其中第0个向量元素对应于向量的物理长度,其他三个(1到3)对应于x, y和z分量。该列表包含约1000000个条目。所以性能是一个问题。我根据向量的长度对列表进行排序。现在我需要过滤列表,以便保留不同长度的向量,如果长度相同,则保留位置1到3上包含不同条目(不是排列)的向量,如示例中所示。如果你需要更多的信息,请告诉我。在过滤过程中不应改变矢量。

问题:如果可能的话,如何使用c#和linq来实现?

的例子:

 0,    0,     0;      0,0000   ->   select 
 0,    1,    -1;      8,2883   ->   select
 1,    0,    -1;      8,2883   ->   not select
 0,   -1,     1;      8,2883   ->   not select
-1,    0,     1;      8,2883   ->   not select
 1,   -1,     0;      8,2883   ->   not select
-1,    1,     0;      8,2883   ->   not select
 1,    1,    -2;     14,3558   ->   select
...
 2,     2,    -5;    38,6145   ->   select
-2,    -2,     5;    38,6145   ->   not select
 1,     4,    -4;    38,6145   ->   select
 4,     1,    -4;    38,6145   ->   not select
-1,    -4,     4;    38,6145   ->   not select
-4,    -1,     4;    38,6145   ->   not select
-1,     4,    -4;    38,6145   ->   not select
 4,    -1,    -4;    38,6145   ->   not select
-4,     1,     4;    38,6145   ->   not select
 1,    -4,     4;    38,6145   ->   not select
-2,     5,    -2;    38,6145   ->   not select
 5,    -2,    -2;    38,6145   ->   not select
 2,    -5,     2;    38,6145   ->   not select
-5,     2,     2;    38,6145   ->   not select
 4,    -4,    -1;    38,6145   ->   not select
-4,     4,    -1;    38,6145   ->   not select
-4,     4,     1;    38,6145   ->   not select
 4,    -4,     1;    38,6145   ->   not select
...
代码:

private static double absm = 0;
private static int[] m = new int[3];
private static int[] m2 = new int[3];
private static List<double[]> ihkl1 = new List<double[]>();
private static List<double[]> ihkl2 = new List<double[]>();
...
private static void init_latt()
{
    for (int i = -kmax[2]; i < kmax[2]; i++ )
    {
        m[2] = i;
        for (int j = -kmax[1]; j < kmax[1]; j++)
        {
            m[1] = j;
            for (int k = -kmax[0]; k < kmax[0]; k++)
            {                        
                m[0] = k;
                absm = calcabsm(metten, m);                                             
                if (absm < gmax)
                {
                    double[] row1 = new double[4];
                    row1[0] = absm;
                    row1[1] = (double)m[0];
                    row1[2] = (double)m[1];
                    row1[3] = (double)m[2];
                    ihkl1.Add(row1);
                }
            }
        }
    }    
    ihkl2 = ihkl1.AsParallel().OrderBy(x => x[0]).ToList();
}
...

c#筛选双精度[]值的列表

首先,我同意Jon Skeet使用类Vector封装那些double数组的建议。之后你可以这样做:

public class VectorEqualityComparer : IEqualityComparer<Vector>
{
    public bool Equals(Vector x, Vector y)
    {
        //here you implement the equality among vectors you defined in your question
    }
    public int GetHashCode(Vector obj)
    {
        //you can return something like obj.InnerArray.GetHashCode()
    }
}

现在,如果您有Vector的列表,即yourList,您可以调用:

var result = yourList.Distinct(new VectorEqualityComparer());

希望这能帮助你实现你想要的。祝你好运! !