fast way to remove lowercase substrings from string?

后端 未结 3 1099
予麋鹿
予麋鹿 2021-01-08 00:40

What\'s an efficient way in Python (plain or using numpy) to remove all lowercase substring from a string s?

s = \"FOObarFOOObBAR\"
remove_lowe         


        
3条回答
  •  孤城傲影
    2021-01-08 01:14

    My first approach would be ''.join(x for x in s if not x.islower())

    If you need speed use mgilson answer, it is a lot faster.

    >>> timeit.timeit("''.join(x for x in 'FOOBarBaz' if not x.islower())")
    3.318969964981079
    
    >>> timeit.timeit("'FOOBarBaz'.translate(None, string.ascii_lowercase)", "import string")
    0.5369198322296143
    
    >>> timeit.timeit("re.sub('[a-z]', '', 'FOOBarBaz')", "import re")
    3.631659984588623
    
    >>> timeit.timeit("r.sub('', 'FOOBarBaz')", "import re; r = re.compile('[a-z]')")
    1.9642360210418701
    
    >>> timeit.timeit("''.join(x for x in 'FOOBarBaz' if x not in lowercase)", "lowercase = set('abcdefghijklmnopqrstuvwxyz')")
    2.9605889320373535
    

提交回复
热议问题