C# 比较两个列表<;int>;
我正在写一个小程序来比较两个列表。如果值相同,我将它们添加到列表DUP,如果它们不同,我将它们添加到distinct。我注意到我的一些值被添加了,而一些值没有添加,在调试了一段时间后,我不确定问题出在哪里。有人能帮我弄点光吗?谢谢C# 比较两个列表<;int>;,c#,C#,我正在写一个小程序来比较两个列表。如果值相同,我将它们添加到列表DUP,如果它们不同,我将它们添加到distinct。我注意到我的一些值被添加了,而一些值没有添加,在调试了一段时间后,我不确定问题出在哪里。有人能帮我弄点光吗?谢谢 List<int> groupA = new List<int>(); List<int> groupB = new List<int>(); List<int&g
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]);
}
List groupA=新列表();
List GROUPBB=新列表();
List dups=新列表();
List distinct=新列表();
A组。添加(2);
A组:增加(24);
A组。添加(5);
A组增加(72);
A组(276);
A组:增加(42);
A组:增加(92);
A组增加(95);
A组:增加(266);
A组:增加(42);
A组:增加(92);
B组增加(5);
B组增加(42);
B组增加(95);
groupA.Sort();
groupB.Sort();
对于(int a=0;a
从列表中删除项目时,会向下移动剩余元素的索引。
本质上,您正在使用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]);
}
到
循环结束,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
}
}
及
请注意,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 dups=groupA.Intersect(groupB.ToList();
List distinct=groupA.Except(groupB.ToList();
(假设我正确理解了您要做的事情)您需要在以下两个方面找到缺少的元素:
List<int> onlyInA = groupA.Except(groupB).ToList();
List<int> onlyInB = groupB.Except(groupA).ToList();
List onlyna=groupA.Except(groupB.ToList();
仅列出inB=groupB.Except(groupA.ToList();
或者在单个linq中:
List<int> missing = groupA.Except(groupB).Union(groupB.Except(groupA)).ToList()
List missing=groupA.Except(groupB).Union(groupB.Except(groupA)).ToList()
注意-与所有linq一样,值得指出的是,这并不是最有效的方法。所有列表迭代都有成本。如果两个列表非常大,则使用一种较长的方法对两个列表进行排序,然后将它们一起迭代会更快…您可以发布预期结果吗?我想您很快就会删除组[b]。因为你的工作就像A1->B1234567等,A2->B123445。如果从b中删除一个数字,它将无法以重复或不同的形式显示。但这只是我的本能您在迭代时从列表中删除元素,因此循环条件(基于元素编号)可能会受到影响…这些人是正确的。在迭代列表时,切勿修改列表。它会产生各种各样的bug。其他新闻:比雷托在做什么?另外,为什么要排序,然后不利用列表已排序的事实?另外:你可以通过使用哈希集而不是列表来提高效率。Eric,你能给我一个哈希列表的示例吗?用while循环替换这两个for循环吗?我将把实现留给你,但我添加了一个示例,说明了你的代码哪里出错以及如何修复。列表的长度很重要。var l1=新列表(){1,5,6};var l2=新列表(){6,5,1,2};l2.除了(l1)=>2。l1.除(l2)=>Nothing外,如上所述:
此方法返回第一个中未出现在第二个中的元素。它不会在第二秒内返回那些在第一个中没有出现的元素。
这意味着如果list2
包含所有list1
的项目和其他项目,list1。除了(list2)
之外,将返回一个空集合。
dups.AddRange(groupA);
dups.AddRange(groupB);
dups = dups.GroupBy(i => i)
.Where(g => g.Count() > 1)
.Select(g => g.Key)
.ToList();
groupA = groupA.Distinct().ToList();
groupB = groupB.Distinct().ToList();
dups = groupA.Intersect(groupB).ToList();
distinct = groupA.Except(groupB).ToList();
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();
List<int> missing = groupA.Except(groupB).Union(groupB.Except(groupA)).ToList()