比较两个 IEnumerable 的内容
是否有一个内置的 LINQ 方法可以用来找出两个序列是否包含相同的项目,而不考虑顺序?
例如:
{1, 2, 3} == {2, 1, 3}
{1, 2, 3} != {2, 1, 3, 4}
{1, 2, 3} != {1, 2, 4}
您有 SequenceEquals
,但是我必须首先 Order
两个序列,不是吗?
Is there a built in LINQ method thing I can use to find out if two sequences contains the same items, not taking the order into account?
For example:
{1, 2, 3} == {2, 1, 3}
{1, 2, 3} != {2, 1, 3, 4}
{1, 2, 3} != {1, 2, 4}
You have the SequenceEquals
, but then I would have to Order
both sequences first, wouldn't I?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
有很多方法。 假设 A 和 B 是 IEnumerable。
There are quite a few ways. Assume A and B is IEnumerable.
如果您不关心重复项(即您认为
{1, 2, 3}
等于{1, 2, 3, 2}
),那么:(或者任何类型是元素类型而不是
int
)。否则:
记录第一个序列中的每个元素,然后对照它检查第二个元素。 当第二个序列中有太多元素时,您可以返回 false,否则,如果计数字典中没有剩余任何元素,则它们相等,如果剩余任何元素,则返回 false。
与使用
OrderBy()
和 O(n) 比较的两种 O(n log n) 类型不同,您有一个 O(n) 操作来构建计数集,并有一个 O (n) 检查。If you don't care about duplicates (i.e. you'd consider
{1, 2, 3}
to be equal to{1, 2, 3, 2}
) then:(Or whatever type is the element type instead of
int
).Otherwise:
Keep a tally of each element in the first sequence, then check the second against it. The moment you have one too many in the second sequence you can return false, otherwise if you have nothing left in the dictionary of tallies they are equal, or false if there's any elements left.
Rather than the two O(n log n) sorts of using
OrderBy()
followed by the O(n) comparison, you've an O(n) operation building the set of tallies, and an O(n) check against it.使用两个 IEnumerables(A 和 B):
我认为这比 except(A).Count 更好,因为不会评估整个 Excep。 一旦在 except 中找到一个元素,它就会停止。 通过计数,可以评估整个 except。
除此之外,我们可以通过首先检查 Count 属性来避免对这些昂贵的 except 进行评估。 如果计数不相等,则我们检查例外。
With two IEnumerables (A and B) :
I think this is better than Except(A).Count because the entire Excep will not be evaluated. It will stop as soon as one element is found in the Except. With the Count, the entire Except is evaluated.
On top of this, we can avoid the evaluation of these costly Except just by checking the Count properties first. If Counts are not Equal, then we check the Excepts.
尝试使用 HashSet 类:
但只有当值不同时,它才能正常工作。
例如
也被认为与上述方法“相等”。
Try the HashSet class:
But that does only work correctly if the values are distinct.
For example
are also considered as "equal" with the mentioned method.
坚持您的示例,您可以将 IEnumerable 都设置为 List 类型,然后使用 SequenceEqual ,如下例所示:
Sticking with your example, you can make both of IEnumerable to be of type List and then use SequenceEqual as the example below:
为了比较两个对象中的数据,我简单地使用了这个
To compare the data in the two objects, I simply used this
我这样做是为了将新项目合并到一个没有重复项的集合中,
它需要两个集合并返回所有没有任何重复项的项目
现在通过删除! 对于 contains 语句,
它将返回重复项
I did this for merging new items into a collection without duplicates,
it takes two collections and returns all the items with out any duplicates
Now by removing the ! for the contains statement
it will return the duplicates
我认为排序顺序是实现这一目标的最快方法。
I think ordering the sequence is the fastest way you can achieve this.
如果您真的只是测试是否有重复项,那么 leppie 的建议应该可行:
但是如果您只需要得到一个没有重复项的 IEnumerable:
If you're really just testing to see if there are duplicates, then leppie's suggestion should work:
But if you just need to arrive at an IEnumerable with no duplicates:
对于发现问题也关心顺序的人来说,这里有一些可能有用的东西。 用于枚举的静态 CompareTo 方法和 IComparer 实现。 之后的测试包括:
For people finding question that also care about order, here is something that may be useful. A static CompareTo method for enumerables and an IComparer implementation. Tests included afterwards: