如何从嵌套列表中删除重复的行?

时间:2018-08-16 13:34:59

标签: c# list

我有嵌套列表可以用作2D数组:

public List<List<float?>> arrayFloatValue = new List<List<float?>>(); 

此嵌套列表的父级有 2000 列,子数组中有 20000 个浮点值。现在,我想从子列表中匹配重复的行删除。下面是示例代码。

//Define capacity
int ColumnsCount = 2000;
int RowsCount = 20000;

//Create object
public List<List<float?>> arrayFloatValue = new List<List<float?>>();

//Initiate parent list i.e. 2000 
arrayFloatValue = new List<float?>[ColumnsCount].ToList();

//Initiate child list i.e. 20000 
for (int i = 0; i < ColumnsCount; i++)
{
    arrayFloatValue[i] = new float?[RowsCount].ToList();
}

//Fill dummy data.
for (int x = 0; x < ColumnsCount; x++)
{
    for (int y = 0; y < RowsCount; y++)
    {
        if (y % 50 != 0)
            arrayFloatValue[x][y] = x + y; // Assign dummy value
        else
            arrayFloatValue[x][y] = 0;     // Forcefully 0 value added for each 50th row.
    }
}

现在我有数组了

//  [0] [0]     [1] [0]     [2] [0]     ...
//      [1]         [2]         [3]     ...
//      [2]         [3]         [4]     ...
//      [3]         [4]         [5]     ...
//      [4]         [5]         [6]     ...
//      [5]         [6]         [7]     ...
//      [6]         [7]         [8]     ...
//      [7]         [8]         [9]     ...
//      [8]         [9]         [10]    ...
//      [9]         [10]        [11]    ...
//  ...         ...         ...
//      [49]        [50]        [51]    ...
//      [0]         [0]         [0] ...
//  
//  And so on..
//  

现在,我要删除每个列中的重复值。在上面的示例中,我在每个行索引处都重复了0的值,例如第50,第100t h,第150个……等等。我要删除这些行。

2 个答案:

答案 0 :(得分:2)

您可以使用自定义 Distinct来尝试旧的IEqualityComparer<T>(我们将把 lists SequenceEqual进行比较):

public class ListComparer<T> : IEqualityComparer<IEnumerable<T>> {
  public bool Equals(IEnumerable<T> x, IEnumerable<T> y) {
    return Enumerable.SequenceEqual(x, y);
  }

  public int GetHashCode(IEnumerable<T> obj) {
    return obj == null ? -1 : obj.Count();
  }
}

现在Distinct

  List<List<float?>> list = new List<List<float?>>() {
    new List<float?>() { 1, 2, 3},
    new List<float?>() { 4, 5, 6, 7},
    new List<float?>() { 1, 2, 3},
    new List<float?>() { null },
    new List<float?>() { 1, 2, null },
    new List<float?>() { null },
    new List<float?>() { 1, 2 },
  };

  var result = list
    .Distinct(new ListComparer<float?>());

  string report = string.Join(Environment.NewLine,
    result.Select(line => $"{string.Join(", ", line)}"));

  Console.Write(report);

结果:

1, 2, 3
4, 5, 6, 7

1, 2, 
1, 2

答案 1 :(得分:1)

如果我理解您的问题,则可以使用HashSet筛选列表。但是您必须定义一个IEqualityComparer>来检查元素的相等性。

我举了一个例子:

using System;
using System.Collections.Generic;
using System.Linq;

namespace MyNamespace
{
    public class Program
    {
        public static void Main()
        {
            List<List<float>> arrayFloatValue = new List<List<float>>
            {
                new List<float> {1, 2, 3},
                new List<float> {1, 3, 2},
                new List<float> {1, 2, 3},
                new List<float> {3, 5, 7}
            };

            var hsArrayFloatValue = new HashSet<List<float>>(arrayFloatValue, new ListComparer());
            List<List<float>> filteredArrayFloatValue = hsArrayFloatValue.ToList();

            DisplayNestedList(filteredArrayFloatValue);

            //output:
            //1 2 3
            //1 3 2
            //3 5 7
        }

        public static void DisplayNestedList(List<List<float>> nestedList)
        {
            foreach (List<float> list in nestedList)
            {
                foreach (float f in list)
                    Console.Write(f + " ");

                Console.WriteLine();
            }

            Console.ReadLine();
        }
    }

    public class ListComparer : IEqualityComparer<List<float>>
    {
        public bool Equals(List<float> x, List<float> y)
        {
            if (x == null && y == null)
                return true;

            if (x == null || y == null || x.Count != y.Count)
                return false;

            return !x.Where((t, i) => t != y[i]).Any();
        }

        public int GetHashCode(List<float> obj)
        {
            int result = 0;

            foreach (float f in obj)
                result |= f.GetHashCode();

            return result;
        }
    }
}

尽管,我不建议您比较浮点数。请改用小数点。