从字符串中删除小写子字符串的快速方法? [英] fast way to remove lowercase substrings from string?
本文介绍了从字符串中删除小写子字符串的快速方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python中(纯文本或使用numpy)从字符串s
中删除所有小写子字符串的有效方法是什么?
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"
推荐答案
我将使用str.translate
.如果您为转换表传递None
,则仅执行删除步骤.在这种情况下,我将ascii_lowercase
作为要删除的字母传递.
I'd use str.translate
. Only the delete step is performed if you pass None
for the translation table. In this case, I pass the ascii_lowercase
as the letters to be deleted.
>>> import string
>>> s.translate(None,string.ascii_lowercase)
'FOOFOOOBAR'
我怀疑您会找到一种更快的方法,但是如果有人有动力,总是有timeit
来比较不同的选择:).
I doubt you'll find a faster way, but there's always timeit
to compare different options if someone is motivated :).
这篇关于从字符串中删除小写子字符串的快速方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文