简体   繁体   English

高效比较python中的两组

[英]Efficiently compare two sets in python

I have two large integer sets set1 and set2 .我有两个大的 integer sets set1set2 Which one of the below is more efficient?以下哪一项效率更高?

Example:例子:

if(set1 == set2)

or或者

if(len(set1)==len(set2))

The two statements are completely different from each other.这两种说法完全不同。

if(set1==set2) compares for equality of each element in both the sets, and evaluates to true if and only if both the sets are exactly same. if(set1==set2)比较两个集合中每个元素的相等性,并且当且仅当两个集合完全相同时才评估为 true。

Whereas if(len(set1)==len(set2)) compares only the length of both the sets.if(len(set1)==len(set2))仅比较两个集合的长度。 Even if you have two sets with same length there may be cases when they are not the same.即使您有两个长度相同的集合,也可能存在不一样的情况。 For eg consider this:例如考虑这个:

set1: [1, 3, 6, 29, 31] set1: [1, 3, 6, 29, 31]

set2: [1, 3, 7, 10, 15] set2: [1, 3, 7, 10, 15]

Though the sets have the same length they are not the same.尽管这些集合具有相同的长度,但它们并不相同。

You could do this to save on time.您可以这样做以节省时间。

if len(set1) == len(set2):
    if set1 == set2:
        //TODO when sets are equal
    else
        //TODO when sets are not equal.
else
    //TODO when sets are not equal

As Nikhil Wagh already explained, in the original question there was a confusion between comparing the length of a set and checking the equality of two sets.正如 Nikhil Wagh 已经解释的那样,在最初的问题中,比较集合的长度和检查两个集合的相等性之间存在混淆。 I just want to add that simple s1 == s2 is enough and is efficient, because __eq__() (and its oppsite __ne__()) method already performs this optimization.我只想补充一点,简单的s1 == s2就足够了,而且效率很高,因为 __eq__() (及其相反的 __ne__()) 方法已经执行了这种优化。 It first checks if the sets have the same length, then it checks if the sets have the same hash, and only then it performs set_issubset check.它首先检查集合是否具有相同的长度,然后检查集合是否具有相同的散列,然后才执行set_issubset检查。

You can find the implementation of set equality check in function set_richcompare() in file setobject.c .您可以在文件setobject.c中的函数set_richcompare()中找到集合相等检查的实现。 You don't need to be an expect in C to understand the algorithm - https://github.com/python/cpython/blob/master/Objects/setobject.c您不需要成为 C 中的期望来理解算法 - https://github.com/python/cpython/blob/master/Objects/setobject.c

比较长度比比较每个元素要快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM