Python efficient way to check if very large string contains a substring

后端 未结 8 1628
無奈伤痛
無奈伤痛 2021-01-02 05:04

Python is not my best language, and so I\'m not all that good at finding the most efficient solutions to some of my problems. I have a very large string (coming from a 30 MB

相关标签:
8条回答
  • I do not see how to make it more optimal on the comparison, to be honest. But you can use less memory and lose less time with I/O if you read the file line by line:

    has_small_string = False
    for line in large_file:
        if small_string in line:
            has_small_string = True
            break
    if has_small_string:
       # ... Your stuff here ...
    

    This is no revolutionary improvement, and can be even less useful if you really need the large string in the memory, but it may be helpful, so I am posting here :)

    0 讨论(0)
  • 2021-01-02 06:08

    If you just want to check if that substring exists,

    for line in open("file"):
        if substring in line:
             print "exists"
             sys.exit() # or break to do some other stuff
    
    0 讨论(0)
提交回复
热议问题