<small id='z6pNQ'></small><noframes id='z6pNQ'>

<tfoot id='z6pNQ'></tfoot>

  • <legend id='z6pNQ'><style id='z6pNQ'><dir id='z6pNQ'><q id='z6pNQ'></q></dir></style></legend>
      • <bdo id='z6pNQ'></bdo><ul id='z6pNQ'></ul>
    1. <i id='z6pNQ'><tr id='z6pNQ'><dt id='z6pNQ'><q id='z6pNQ'><span id='z6pNQ'><b id='z6pNQ'><form id='z6pNQ'><ins id='z6pNQ'></ins><ul id='z6pNQ'></ul><sub id='z6pNQ'></sub></form><legend id='z6pNQ'></legend><bdo id='z6pNQ'><pre id='z6pNQ'><center id='z6pNQ'></center></pre></bdo></b><th id='z6pNQ'></th></span></q></dt></tr></i><div id='z6pNQ'><tfoot id='z6pNQ'></tfoot><dl id='z6pNQ'><fieldset id='z6pNQ'></fieldset></dl></div>

        测试两个IEnumerable&lt;T&gt;具有相同频率的相同值

        Test whether two IEnumerablelt;Tgt; have the same values with the same frequencies(测试两个IEnumerablelt;Tgt;具有相同频率的相同值)

        <small id='7Odi9'></small><noframes id='7Odi9'>

      1. <tfoot id='7Odi9'></tfoot><legend id='7Odi9'><style id='7Odi9'><dir id='7Odi9'><q id='7Odi9'></q></dir></style></legend>
            <tbody id='7Odi9'></tbody>

              <bdo id='7Odi9'></bdo><ul id='7Odi9'></ul>
            • <i id='7Odi9'><tr id='7Odi9'><dt id='7Odi9'><q id='7Odi9'><span id='7Odi9'><b id='7Odi9'><form id='7Odi9'><ins id='7Odi9'></ins><ul id='7Odi9'></ul><sub id='7Odi9'></sub></form><legend id='7Odi9'></legend><bdo id='7Odi9'><pre id='7Odi9'><center id='7Odi9'></center></pre></bdo></b><th id='7Odi9'></th></span></q></dt></tr></i><div id='7Odi9'><tfoot id='7Odi9'></tfoot><dl id='7Odi9'><fieldset id='7Odi9'></fieldset></dl></div>

                • 本文介绍了测试两个IEnumerable&lt;T&gt;具有相同频率的相同值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有两个多重集,都是 IEnumerables,我想比较它们.

                  I have two multisets, both IEnumerables, and I want to compare them.

                  string[] names1 = { "tom", "dick", "harry" };
                  string[] names2 = { "tom", "dick", "harry", "harry"};
                  string[] names3 = { "tom", "dick", "harry", "sally" };
                  string[] names4 = { "dick", "harry", "tom" };

                  希望 names1 == names4 返回 true(而 self == self 显然返回 true)
                  但所有其他组合都返回 false.

                  Want names1 == names4 to return true (and self == self returns true obviously)
                  But all other combos return false.

                  什么是最有效的方法?这些可以是大量复杂对象.

                  What is the most efficient way? These can be large sets of complex objects.

                  我看着做:
                  var a = name1.orderby(v => v.Name);
                  var b = name4.orderby(v => v.Name);

                  return a == b;

                  推荐答案

                  最有效的方法取决于数据类型.一个相当有效且非常短的 O(N) 解决方案如下:

                  The most efficient way would depend on the datatypes. A reasonably efficient O(N) solution that's very short is the following:

                  var list1Groups=list1.ToLookup(i=>i);
                  var list2Groups=list2.ToLookup(i=>i);
                  return list1Groups.Count == list2Groups.Count 
                     && list1Groups.All(g => g.Count() == list2Groups[g.Key].Count());
                  

                  项目必须具有有效的 EqualsGetHashcode 实现.

                  The items are required to have a valid Equals and GetHashcode implementation.

                  如果您想要一个更快的解决方案,cdhowie 的解决方案在 10000 个元素下相当快,并且领先大型简单对象集合的因子 5 - 可能是由于更好的内存效率.

                  If you want a faster solution, cdhowie's solution below is comparably fast @ 10000 elements, and pulls ahead by a factor 5 for large collections of simple objects - probably due to better memory efficiency.

                  最后,如果您真的对性能感兴趣,我肯定会尝试 Sort-then-SequenceEqual 方法.虽然它的复杂性更差,但这只是一个 log N 因素,并且这些因素肯定会被所有实际数据集大小的常数差异所淹没 - 你也许可以就地排序,使用数组甚至增量排序(可以是线性的).即使有 40 亿个元素,log-base-2 也只有 32;这是一个相关的性能差异,但常数因子的差异可能会更大.例如,如果您正在处理整数数组并且不介意修改收集顺序,那么即使对于 10000000 个项目(两倍,我在 32 位上得到 OutOfMemory),以下选项也比任何一个选项都快:

                  Finally, if you're really interested in performance, I'd definitely try the Sort-then-SequenceEqual approach. Although it has worse complexity, that's just a log N factor, and those can definitely be drowned out by differences in the constant for all practical data set sizes - and you might be able to sort in-place, use arrays or even incrementally sort (which can be linear). Even at 4 billion elements, the log-base-2 is just 32; that's a relevant performance difference, but the difference in constant factor could conceivably be larger. For example, if you're dealing with arrays of ints and don't mind modifying the collection order, the following is faster than either option even for 10000000 items (twice that and I get an OutOfMemory on 32-bit):

                  Array.Sort(list1);
                  Array.Sort(list2);
                  return list1.SequenceEqual(list2);
                  

                  YMMV 取决于机器、数据类型、月球周期和其他影响微基准的常见因素.

                  YMMV depending on machine, data-type, lunar cycle, and the other usual factors influencing microbenchmarks.

                  这篇关于测试两个IEnumerable&lt;T&gt;具有相同频率的相同值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding and removing users from Active Directory groups in .NET(在 .NET 中的 Active Directory 组中添加和删除用户)
                  set equality in linq(在 linq 中设置相等)
                  HashSet conversion to List(HashSet 转换为 List)
                  How to set timeout for webBrowser navigate event(如何为 webBrowser 导航事件设置超时)
                  How do you determine if two HashSets are equal (by value, not by reference)?(您如何确定两个 HashSet 是否相等(按值,而不是按引用)?)
                  Shorthand Accessors and Mutators(速记访问器和突变器)

                • <legend id='kfnhJ'><style id='kfnhJ'><dir id='kfnhJ'><q id='kfnhJ'></q></dir></style></legend>

                    <tbody id='kfnhJ'></tbody>

                    <tfoot id='kfnhJ'></tfoot>
                      • <bdo id='kfnhJ'></bdo><ul id='kfnhJ'></ul>
                        <i id='kfnhJ'><tr id='kfnhJ'><dt id='kfnhJ'><q id='kfnhJ'><span id='kfnhJ'><b id='kfnhJ'><form id='kfnhJ'><ins id='kfnhJ'></ins><ul id='kfnhJ'></ul><sub id='kfnhJ'></sub></form><legend id='kfnhJ'></legend><bdo id='kfnhJ'><pre id='kfnhJ'><center id='kfnhJ'></center></pre></bdo></b><th id='kfnhJ'></th></span></q></dt></tr></i><div id='kfnhJ'><tfoot id='kfnhJ'></tfoot><dl id='kfnhJ'><fieldset id='kfnhJ'></fieldset></dl></div>

                          <small id='kfnhJ'></small><noframes id='kfnhJ'>