簡體   English   中英

比較兩個列表<int>

[英]Compare two List<int>

我正在編寫一個小程序來比較兩個列表。 如果值相同,我將它們添加到列表重復,如果它們不同,我將它們添加到不同。 我注意到我的一些值被添加了一些沒有,經過調試一段時間后,我不確定問題是什么。 有人可以照亮一點嗎? 謝謝。

        List<int> groupA = new List<int>();
        List<int> groupB = new List<int>();

        List<int> dups = new List<int>();
        List<int> distinct = new List<int>();

        groupA.Add(2);
        groupA.Add(24);
        groupA.Add(5);
        groupA.Add(72);
        groupA.Add(276);
        groupA.Add(42);
        groupA.Add(92);
        groupA.Add(95);
        groupA.Add(266);
        groupA.Add(42);
        groupA.Add(92);


        groupB.Add(5);
        groupB.Add(42);
        groupB.Add(95);

        groupA.Sort();
        groupB.Sort();

        for (int a = 0; a < groupA.Count; a++)
        {
            for (int b = 0; b < groupB.Count; b++)
            {
                groupA[a].CompareTo(groupB[b]);


                if (groupA[a] == groupB[b])
                {
                    dups.Add(groupA[a]);
                    groupA.Remove(groupA[a]);
                    groupB.Remove(groupB[b]);
                }

            }
            distinct.Add(groupA[a]);
        }

我會使用IntersectExcept方法:

dups = groupA.Intersect(groupB).ToList();
distinct = groupA.Except(groupB).ToList();

從列表中刪除項目時,會將剩余元素的索引向下移動。 本質上,您是在使用 for 循環跳過一些項目。
嘗試使用 while 循環,並在不刪除項目時手動增加計數器。

例如下面的代碼是不正確的

List<int> nums = new List<int>{2, 4, 6, 7, 8, 10, 11};

for (int i = 0; i < nums.Count; i++)
{
  if (nums[i] % 2 == 0)
    nums.Remove(nums[i]);
}

If 將返回列表{4, 7, 10, 11}而不僅僅是{7, 11}

它不會刪除 4 的值,因為當我刪除 2 的值時,(對於i=0nums列表從

//index 0  1  2  3  4   5   6 
nums = {2, 4, 6, 7, 8, 10, 11}

//index 0  1  2  3  4   5
nums = {4, 6, 7, 8, 10, 11}

循環結束, i 增加到 1,引用的下一項是nums[1] ,它不是直觀預期的 4,而是 6。因此實際上跳過了 4 的值,並且檢查不是執行。

每次修改正在迭代的集合時,您都應該非常非常小心。 例如,如果您嘗試這樣做, foreach語句將拋出異常。 在這種情況下,您可以使用一段時間

List<int> nums = new List<int>{2, 4, 6, 7, 8, 10, 11};

int i = 0;
while (i < nums.Count)
{
  if (nums[i] % 2 == 0)
  {
    nums.Remove(nums[i])
  }      
  else
  {
    i++; //only increment if you are not removing an item
         //otherwise re-run the loop for the same value of i
  }  
}

你甚至可以分叉 for,比如

for (int i = 0; i < nums.Count; i++)
{
  if (nums[i] % 2 == 0)
  {
    nums.Remove(nums[i]);
    i--; //decrement the counter, so that it will stay in place
         //when it is incremented at the end of the loop
  }
}

或者,您可以使用 linq,如下所示:

distinct.AddRange(groupA);
distinct.AddRange(groupB);
distinct = distinct.Distinct().ToList();

dups.AddRange(groupA);
dups.AddRange(groupB);

dups = dups.GroupBy(i => i)
           .Where(g => g.Count() > 1)
           .Select(g => g.Key)
           .ToList();

請注意,LINQ 代碼不會改變您現有的 groupA 和 groupB 列表。 如果你只是想區分它們,你可以這樣做

groupA = groupA.Distinct().ToList();
groupB = groupB.Distinct().ToList();

您可以使用 Linq 輕松完成:

    List<int> dups = groupA.Intersect(groupB).ToList();
    List<int> distinct = groupA.Except(groupB).ToList();

(假設我正確理解你想要做什么)

您需要在兩者中找到缺失的元素:

List<int> onlyInA = groupA.Except(groupB).ToList();
List<int> onlyInB = groupB.Except(groupA).ToList();

或者在單個 linq 中:

List<int> missing = groupA.Except(groupB).Union(groupB.Except(groupA)).ToList()

注意 - 與所有 linq 一樣,值得指出的是,這不是最有效的方法。 所有列表迭代都有成本。 如果列表真的很大,那么對兩個列表進行排序然后將它們一起迭代的更長時間的方法會更快......

問題的標題是“比較兩個列表”,一些對真/假結果感興趣的人會登陸這個問題

使用 Enumerable.SequenceEqual 方法

    if (listA.SequenceEqual(listB))
    {
       // they are equal
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM