简体   繁体   English

关于“ in” Python时间复杂度的简单查询

[英]Simple query about time complexity for Python “in”

I have a function strip_punctuation(text) which takes in a string of text and removes all punctuation in it using a punctuation list. 我有一个函数strip_punctuation(text),它接收一个文本字符串,并使用标点符号列表删除其中的所有标点符号。 I am not sure about the time complexity whether it is O(N)*N or O(N^2). 我不确定时间复杂度是O(N)* N还是O(N ^ 2)。 I think that it works where it is O(N) for N text length, and then O(N) for length of punctuation. 我认为这适用于N个文本长度为O(N),然后为标点符号长度为O(N)的情况。 May someone clarify the time complexity for this code? 有人可以澄清这段代码的时间复杂度吗?

def strip_punctuation(text):
    punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
    stripped = ""
    for i in text:
        if i not in punctuations:
            stripped = stripped + i

    return stripped

If N is len(text) , then this is O(N): 如果N为len(text) ,则为O(N):

for i in text

If M is len(punctuations) , then this code is O(M^2): 如果M为len(punctuations) ,则此代码为O(M ^ 2):

if i not in punctuations:
    stripped = stripped + i

That is because the whole stripped (which has length >= M) has to be copied M times ( stripped + i makes a copy of stripped ). 这是因为整个stripped (长度> = M)必须被复制M次( stripped + i构成stripped的副本)。

So, if both text and punctuations were inputs, the complexity would be O(N) * O(M^2), but in this case, M is a constant, so the complexity is O(N) . 因此,如果同时输入了textpunctuations ,则复杂度为O(N)* O(M ^ 2),但是在这种情况下,M为常数,因此复杂度为O(N)

Note that if punctuations was very very big, the function would be very very slow, but its complexity would still be just O(N), which only means that it is N times slower when the input is N times bigger. 请注意,如果punctuations非常大,该函数将非常慢,但其复杂度仍仅为O(N),这仅意味着当输入大N倍时,它的速度慢N倍。

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

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