python中的'in'运算符功能 [英] 'in' operator functionality in python
问题描述
我需要删除 string1
中出现在 string2
中的字符.这里 string1
和 string2
只有小写字符 a-z,条件是 string1
的长度每次都会更大.
我使用了 in
运算符:
def removeChars (string1, string2):对于字符串 2 中的字符:如果字符串 1 中的字符:string1 = string1.replace(char, '')返回字符串 1
但我在 Stack Overflow 上读到一个 answer 内容:
<块引用>对于list、tuple、set、frozenset、dict或collections.deque等容器类型,表达式x in y
等价于any(x is e or x ==e for e in y)
.
这意味着 in
操作符在幕后使用了 for
循环.
所以我的问题是,在我的代码中的 for
循环中,我是否应该考虑使用嵌套的 for
循环,作为 in
> 运算符在后台使用 for
循环?如果是,这个程序的时间复杂度是多少?
in
不一定在幕后使用循环.例如:
r = range(100000000000)print(333 in r) # 立即打印 True 而不循环
如果您要循环 r
将花费相当长的时间,很明显这不会发生.
in
基本上调用(在幕后)对象的 __contains__
方法.对于某些迭代器,它实际上会循环"通过一切,但并非总是如此.
这个例子和调用基本一样:
r.__contains__(333)
正如评论中所指出的 - str
对象特别具有比普通循环更智能的算法,如您所见 这里
另见示例答案此处>
并查看文档此处
因为现实世界的场景可能意味着 string1
可以是任意长的,但是要删除的字符将是一个有限的小集合,所以加起来可能会更有效率不在 string2
中的所有字符.像这样:
def removeChars (string1, string2):结果 = ''对于字符串 1 中的字符:如果字符不在 string2 中:结果 += 字符返回结果
这将涉及仅循环一次 string1
,但使用 in
对 string2
进行多次检查.这可以进一步简化(以避免 +=
对结果的循环):
def removeChars (string1, string2):return ''.join(char for char in string1 如果 char 不在 string2 中)
I needed to remove the characters in string1
which are present in string2
. Here string1
and string2
have only the lower case characters a-z with given condition that the length of string1
will be greater every time.
I was using the in
operator:
def removeChars (string1, string2):
for char in string2:
if char in string1:
string1 = string1.replace(char, '')
return string1
But I read one answer on Stack Overflow which says:
For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression
x in y
is equivalent toany(x is e or x == e for e in y)
.
Which means the in
operator is using a for
loop behind the scenes.
So my question is, in the for
loop in my code, should I consider that nested for
loops are being used, as the in
operator is using a for
loop in the background? If yes, what would be the time complexity of this program?
in
does not necessarily use loops behind the scenes. For example:
r = range(100000000000)
print(333 in r) # prints True immediately without looping
If you were to loop r
it will take quite a long time, so clearly that doesn't happen.
in
basically calls (behind the scenes) the object's __contains__
method. For some iterators it will in fact "loop" through everything but that's not always the case.
This example is basically the same as calling:
r.__contains__(333)
As pointed out in comments - the str
objects specifically have a smarter algorithm than just plain loops, as you can see here
Also see example answer here
And see the documentation here
Because the real world scenario would probably mean that string1
can be arbitrarily long, but the characters to be removed would be a finite and small set, it will probably be much more efficient to just add up all the characters that aren't in string2
. Something like this:
def removeChars (string1, string2):
result = ''
for char in string1:
if char not in string2:
result += char
return result
This will involve looping over string1
just once, but multiple checks against string2
using in
. This can be further simplified (to avoid +=
loop over the result):
def removeChars (string1, string2):
return ''.join(char for char in string1 if char not in string2)
这篇关于python中的'in'运算符功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!