Remove empty strings from a list of strings

后端 未结 12 1450
孤城傲影
孤城傲影 2020-11-22 04:33

I want to remove all empty strings from a list of strings in python.

My idea looks like this:

while \'\' in str_list:
    str_list.remove(\'\')
         


        
12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 05:22

    Sum up best answers:

    1. Eliminate emtpties WITHOUT stripping:

    That is, all-space strings are retained:

    slist = list(filter(None, slist))
    

    PROs:

    • simplest;
    • fastest (see benchmarks below).

    2. To eliminate empties after stripping ...

    2.a ... when strings do NOT contain spaces between words:

    slist = ' '.join(slist).split()
    

    PROs:

    • small code
    • fast (BUT not fastest with big datasets due to memory, contrary to what @paolo-melchiorre results)

    2.b ... when strings contain spaces between words?

    slist = list(filter(str.strip, slist))
    

    PROs:

    • fastest;
    • understandability of the code.

    Benchmarks on a 2018 machine:

    ## Build test-data
    #
    import random, string
    nwords = 10000
    maxlen = 30
    null_ratio = 0.1
    rnd = random.Random(0)                  # deterministic results
    words = [' ' * rnd.randint(0, maxlen)
             if rnd.random() > (1 - null_ratio)
             else
             ''.join(random.choices(string.ascii_letters, k=rnd.randint(0, maxlen)))
             for _i in range(nwords)
            ]
    
    ## Test functions
    #
    def nostrip_filter(slist):
        return list(filter(None, slist))
    
    def nostrip_comprehension(slist):
        return [s for s in slist if s]
    
    def strip_filter(slist):
        return list(filter(str.strip, slist))
    
    def strip_filter_map(slist): 
        return list(filter(None, map(str.strip, slist))) 
    
    def strip_filter_comprehension(slist):  # waste memory
        return list(filter(None, [s.strip() for s in slist]))
    
    def strip_filter_generator(slist):
        return list(filter(None, (s.strip() for s in slist)))
    
    def strip_join_split(slist):  # words without(!) spaces
        return ' '.join(slist).split()
    
    ## Benchmarks
    #
    %timeit nostrip_filter(words)
    142 µs ± 16.8 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
    
    %timeit nostrip_comprehension(words)
    263 µs ± 19.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    
    %timeit strip_filter(words)
    653 µs ± 37.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    
    %timeit strip_filter_map(words)
    642 µs ± 36 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    
    %timeit strip_filter_comprehension(words)
    693 µs ± 42.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    
    %timeit strip_filter_generator(words)
    750 µs ± 28.6 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    
    %timeit strip_join_split(words)
    796 µs ± 103 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
    

提交回复
热议问题