我应该如何快速比较两个通用列表,以及检查它们是否相同的内容?
//在这里,我有一个列表
List<List<T>> SelectionList = new List<List<T>>();
//我当前的代码比较列表
for(int i = 0; i < SelectionList.Count; i++)
{
for(int j = 0; j < SelectionList.Count; j++)
{
if (SelectionList[i].Equals(SelectionList[j]))
{
SelectionList.Remove(SelectionList[j]);
}
}
}
// 注意:上面的代码据称有效,以防万一如果两个列表的内容均已排序,即它们处于相同的索引,但是在我的列表中,它们可能相同,但列表内容被调整。在这种情况下,它无法识别。
//基本上,我需要删除列表列表中同一列表的任何重复;
//Here I have a List of Lists
List<List<T>> SelectionList = new List<List<T>>();
//My current code to compare lists
for(int i = 0; i < SelectionList.Count; i++)
{
for(int j = 0; j < SelectionList.Count; j++)
{
if (SelectionList[i].Equals(SelectionList[j]))
{
SelectionList.Remove(SelectionList[j]);
}
}
}
//Note: The above code supposedly works, in cases where the contents of both the lists are ordered ie., they are in same index, but in my lists they might be same but list contents are shuffled. in this case it fails to identify.
//Basically I need to remove any repetitions of same list in my list of lists;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
if(and仅考虑)以下内容是正确的:
t
的类型 iComparable andgethashcode(gethashcode()< /code>正确地
,您可以删除与早期列表相匹配的每个
列表:
!lists [i] .except(lists [j])。任何()
。让我们将其分解为:
lists [i] .except(lists [j])
: - 这会产生lists [i]
的所有元素的顺序列表[J]
,无论订单如何。因此,如果
列表中的所有项目[j]
也都在列表中[J]
中,则会产生一个空序列;否则,它将产生非空序列。.yany()()
将返回非空序列的true ,而对于空序列的false
。因此
lists [i] .except(lists [j])。任何()
将返回false
如果项目在每个列表中相同,true
true 代码>如果它们有所不同。这与我们想要的
lists.removeat()
的相反,因此我们只是否定结果,提供最终代码!lists [i] .except(lists [j])。任何()
。编译控制台应用程序:
在dotnetfiddle上尝试: https://dotnetfiddle.net/nwnocp
If (and only if) the following is true:
T
of your list elements implementsIComparable
andGetHashCode()
correctlyThen you can remove each list that matches an earlier list like so (note that you must traverse the list backwards when removing items from the end of it otherwise the loop indices could go out of range):
The important line here is:
!lists[i].Except(lists[j]).Any()
.Let's break it down:
lists[i].Except(lists[j])
: - This produces a sequence of all the elements oflists[i]
that are NOT inlists[j]
, regardless of order.Thus if all of the items in
lists[j]
are also inlists[j]
, this will produce an empty sequence; otherwise, it will produce a non-empty sequence.The
.Any()
will returntrue
for a non-empty sequence, andfalse
for an empty sequence.So
lists[i].Except(lists[j]).Any()
will returnfalse
if the items are the same in each list andtrue
if they differ.This is the opposite of what we want for the
lists.RemoveAt()
so we just negate the result, giving the final code!lists[i].Except(lists[j]).Any()
.Compilable console app:
Try it on DotNetFiddle: https://dotnetfiddle.net/nWnOcP
如果对于每个列表,每个可能的值最多出现在列表中一次,则可以使用
dictionary&lt; t,int&gt;
存储元素出现的频率。然后,您执行以下步骤:k
,检查字典是否包含它作为键。如果没有,则将键k
添加为value 1 到您的字典中。如果是这样,请通过1
来递增键k
的值。2
(如果您有两个列表)或n
(如果您有n
列表)。If, for each list, each possible value appears at most once in the list, you could use a
Dictionary<T,int>
to store how often an element appears. Then you perform the following steps:k
, check if the dictionary contains it as a key. If it does not, then add keyk
with value1
to your dictionary. If it does, then increment the value for keyk
by1
.2
(if you have two lists) orn
(if you haven
lists).正如您所说,您的方式是不正确的,请尝试以下操作:
您应该在列表上迭代以下程序;
Your way is not correct, as you said, try this:
you should iterate the following procedure over your list-Of-Lists;