简体   繁体   English

“如果不在”时间复杂度

[英]'if not in' time complexity

Could someone explain the time complexity of the following loop? 有人可以解释以下循环的时间复杂度吗?

for x in iterable:
    if x not in other_iterable:
        return False

I found a really good Python operation time complexity text lecture here , and saw that the time for the outer for loop was O(N). 我在这里找到了一个非常好的Python操作时间复杂度文本讲座,并且看到外部for循环的时间为O(N)。 However, how does the if x not in other_iterable part factor into the time complexity? 但是, if x not in other_iterable部分中如何影响时间复杂度? I imagine the loop will be checking x against every element in iterable until it is found, or the list is exhausted. 我猜想循环将针对iterable每个元素检查x ,直到找到它,或者列表用完为止。 So what would be the recommended way to make the if x not in other_iterable loop take the smallest amount of time possible? 那么, if x not in other_iterable循环中if x not in other_iterable可能要花费最短的时间, if x not in other_iterable推荐的方法是什么? Possibly having sorted the other_iterable ? 可能已经对other_iterable进行了other_iterable I'm practically a rookie at understanding time complexity, and would like to know more. 我实际上是一位了解时间复杂性的菜鸟,并且想了解更多。

Edit: other_iterable would be a list with possible duplicates. 编辑: other_iterable将是可能重复的列表。

In the question you've called them iterable so I'm going to assume they're not set or similar and that to determine if x not inother_iterable is true you have to check the values in other_iterable one at a time. 在这个问题中,您称它们为iterable因此我将假定它们未set或相似,并且要确定x not inother_iterable是否为真,您必须一次检查other_iterable一个值。 For example, this would be the case if they were lists or generators. 例如,如果它们是列表或生成器,情况就是这样。

Time complexity is about the worst case; 时间复杂度大约是最坏的情况。 it's an upper bound. 这是一个上限 So, in this case the worst case would be in everything in iterable was in other_iterable but was the last item returned. 因此,在这种情况下,最糟糕的情况是iterable中的所有内容都在other_iterable但最后返回的是项目。 Then, for each of the n items in iterable you'd check each of the m items in other_iterable and the total number of operations would be O(n*m) . 然后,对于iterablen项目中的每一个,您将检查other_iterable中的m项目中的每一个,并且操作总数为O(n*m) If n is roughly the same size then it's O(n^2) . 如果n大小大致相同,则为O(n^2)

For example, if iterable = [8, 8, 8] and other_iterable = [1, 2, 3, 4, 5, 6, 7, 8] then for each of the 3 items in iterable you have to check the 8 items in other_iterable until you find out that your if statement is false so you'd have 8 * 3 total operations. 例如,如果iterable = [8, 8, 8] other_iterable = [1, 2, 3, 4, 5, 6, 7, 8] iterable = [8, 8, 8]other_iterable = [1, 2, 3, 4, 5, 6, 7, 8]那么对于iterable中的3个项目,您必须检查以下8个项目: other_iterable直到您发现if语句为false为止,这样总共可以进行8 * 3次运算。

The best case scenario would be if the first item in iterable was not in other_iterable . 最好的情况是,如果iterable的第一项不在other_iterable Then you'd only examine a single element of iterable but you'd iterate over all m items in other_iterable until you learned that the if condition was true and you'd be done. 然后,您将只检查iterable的单个元素,但other_iterable所有m项目进行迭代,直到您了解if条件为true并完成。 That's a total of m operations. 总共进行了m操作。 However, as noted above, big-O time complexity is about worst case scenarios so you wouldn't ordinarily quote this as the complexity. 但是,如上所述,big-O时间复杂度是最坏的情况,因此通常不会将其称为复杂度。

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

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