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

预科

我有嵌套列表作为二维数组执行:

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在每个行索引处都有重复的值,例如50th, 100th 150th .... 等等。我想删除这些行。

德米特里·比琴科

您可以尝试美好的旧Distinct习俗 IEqualityComparer<T>(我们要比较列表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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章