What's an efficient way in Python (plain or using numpy) to remove all lowercase substring from a string s
?
s = "FOObarFOOObBAR"
remove_lower(s) => "FOOFOOBAR"
What's an efficient way in Python (plain or using numpy) to remove all lowercase substring from a string s
?
s = "FOObarFOOObBAR"
remove_lower(s) => "FOOFOOBAR"
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.
I'd use
str.translate
. Only the delete step is performed if you passNone
for the translation table. In this case, I pass theascii_lowercase
as the letters to be deleted.I doubt you'll find a faster way, but there's always
timeit
to compare different options if someone is motivated :).