【问题标题】:How to compare arrays in a list [closed]如何比较列表中的数组[关闭]
【发布时间】:2014-01-15 03:32:30
【问题描述】:

我有一个包含各种大小的数组的列表。例如:

  • 0和7.数组有相同的数据{1,2,3,4,5,6}

  • 2、4、5.数组有相同的数据{1,2,3}

  • 注意! 3和6的数据不一样[3] = {1,2} , [6] = {1,3}

我想获取哪些 indexes 具有相同的数据,并将此索引添加到另一个 List。例如

anotherList[ 0 ] = {0,7}

anotherList[ 1 ] = {2,4,5}

我该怎么做?

提前致谢。

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 我想您会遍历您的列表,并在列表中的其他元素之间进行嵌套循环,比较每个元素并在每次比较找到匹配项时将索引添加到另一个列表。比较本身将是一种接受两个数组并根据您定义的逻辑确定它们是否“相等”的方法。你有尝试过吗?
  • 你想创建一个新的List 那个contains 请求的值;例如anotherList[0] = {0,7} 将返回一个新的List,它会告诉您List contains 两者都是07???
  • 为了简化代码,我建议查看 ISet 实现,如 HashSet 与比较项目的方法,例如 Intersect 返回两个列表中包含的项目msdn.microsoft.com/de-de/library/vstudio/…

标签: c# arrays list compare


【解决方案1】:

试试这个

List<byte[]> anotherList = new List<byte[]>();
foreach (byte[] array in list2)
    if (!list1.Any(a => a.SequenceEqual(array)))
        anotherList.Add(array);

【讨论】:

    【解决方案2】:

    您可以使用此代码(Linq 即 SequenceEqual 在这里非常有用):

        private static IList<IList<int>> EqualArrays(List<int[]> list) {
          IList<IList<int>> result = new List<IList<int>>();
    
          HashSet<int> proceeded = new HashSet<int>();
    
          for (int i = 0; i < list.Count; ++i) {
            if (proceeded.Contains(i))
              continue;
    
            int[] item = list[i];
            List<int> equals = new List<int>() { i };
    
            result.Add(equals);
    
            for (int j = i + 1; j < list.Count; ++j)
              if (item.SequenceEqual(list[j])) {
                equals.Add(j);
                proceeded.Add(j);
              }
          }
    
          return result;
        }
    
    
       ...
    
       // Your test case:
       List<int[]> list = new List<int[]>() {
         new int[] {1, 2, 3, 4, 5, 5, 7},
         new int[] {1},
         new int[] {1, 2, 3},
         new int[] {1, 2},
         new int[] {1, 2, 3},
         new int[] {1, 2, 3},
         new int[] {1, 3},
         new int[] {1, 2, 3, 4, 5, 5, 7}
       };
    
       // anotherList == {{0, 7}, {1}, {2, 4, 5}, {3}, {6}}
       IList<IList<int>> anotherList = EqualArrays(list);
    

    【讨论】:

    • 非常感谢。此解决方案运行良好。
    【解决方案3】:
    int[][] original =
    {
        new [] {1,2,3,4,5,6},
        new [] {1},
        new [] {1,2,3},
        new [] {1,2},
        new [] {1,2,3},
        new [] {1,2,3},
        new [] {1,3},
        new [] {1,2,3,4,5,6},
    };
    
    int[][] anotherList =
        original.Select((values, index) => new { values, index })
                .GroupBy(x => x.values, SequenceComparer<int>.Default)
                .Where(grouping => grouping.Count() > 1)   // optional
                .Select(grouping => grouping.Select(x => x.index).ToArray())
                .ToArray();
    

    我已经从here(和here)修改了SequenceComparer&lt;T&gt; 的定义:

    public class SequenceComparer<T> : IEqualityComparer<IEnumerable<T>>
    {
        public static readonly SequenceComparer<T> Default = new SequenceComparer<T>();
    
        public bool Equals(IEnumerable<T> x, IEnumerable<T> y)
        {
            if (Object.ReferenceEquals(x, y))
                return true;
    
            return x != null && y != null && x.SequenceEqual(y);
        }
    
        public int GetHashCode(IEnumerable<T> seq)
        {
            if (seq == null)
                return 0;
    
            unchecked
            {
                const int p = 16777619;
                const int hash = (int)2166136261;
    
                return seq.Select(e => e.GetHashCode())
                          .Aggregate(hash, (a, b) => (a ^ b) * p));
            }
        }
    }
    

    【讨论】:

    • SequenceComparer 存储在哪个程序集或命名空间中?
    • @OndrejJanacek:编辑答案以包括定义和来源。
    • 我以为你编的。我不知道 .NET 中有这样的事情。
    猜你喜欢
    • 2015-01-27
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    相关资源
    最近更新 更多