[英]Python: what's a more compact/efficient way to strip string of a suffix from list?
Is there a more compact and efficient way of stripping a string of any suffix from a given list, ie: 是否有一种更紧凑,更有效的方式从给定列表中剥离任何后缀的字符串,即:
sfxs = ['suffix1', 'sfx2', 'suffix333']
s = 'string-to-process-sfx2'
for sfx in sfxs:
i = s.find(sfx)
if not i == -1:
s = s[:i]
break
Suffixes are of different lengths 后缀的长度不同
You may use re.sub
. 您可以使用re.sub
。
>>> import re
>>> sfxs = ['suffix1', 'sfx2', 'suffix333']
>>> s = 'string-to-process-sfx2'
>>> re.sub(r'(' + '|'.join(sfxs) + r')$', '',s)
'string-to-process-'
>>> re.sub(r'\b(' + '|'.join(sfxs) + r')$', '',s)
'string-to-process-'
>>> re.sub(r'-(' + '|'.join(sfxs) + r')$', '',s)
'string-to-process'
'|'.join(sfxs)
helps to join the suffix
list with |
'|'.join(sfxs)
有助于使用|
来连接suffix
列表 as delimiter. 作为分隔符。 So r'(' + '|'.join(sfxs) + r')$'
would form a regex like (suff1|suff2|suff3|..)$
. 因此r'(' + '|'.join(sfxs) + r')$'
将形成一个正则表达式,如(suff1|suff2|suff3|..)$
。 Note that $
anchor, which matches the end of the line. 请注意, $
锚点与行尾匹配。 So this would do matching only at the end. 因此,这只会在最后进行匹配。
>>> re.sub(r'(' + '|'.join(sorted(sfxs, key=lambda x:len(x), reverse=True)) + r')$', '',s)
'string-to-process-'
sfxs = ['suffix1', 'sfx2', 'suffix333']
s = 'string-to-process-sfx2'
for sfx in sfxs:
if s.endswith(sfx):
s = s[:-len(sfx)]
break
print(s)
is slightly more efficient because the string comparison looks at the end of the string only. 效率更高一点,因为字符串比较仅在字符串的末尾查找。
sfxs = ['suffix1', 'sfx2', 'suffix333']
s = 'string-to-process-sfx2'
for sfx in sfxs:
if sfx in s:
s.replace(sfx, "")
Should do it. 应该做。 Check to ensure the suffix is in the string, then remove it if it is. 检查并确保后缀在字符串中,然后将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.