简体   繁体   English

Python:从列表中剥离后缀字符串的更紧凑/更有效的方法是什么?

[英]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.

相关问题 什么是在Python中从大字符串中删除空格的简单且高效存储的方法 - What is a simple and memory efficient way strip whitespace from a large string in Python 在Python字符串中剥离某些html标签的最快方法是什么? - What's the fastest way to strip certain html tags in a Python string? 删除列表中连续整数的最快,更有效的方法是什么? - What's the quickest and more efficient way to remove consecutive integers in list? 如果字符串包含列表中的后缀,如何从字符串中删除该特定后缀? - If a string contains a suffix from a list, how do I strip that specific suffix from the string? Python:分解字符串并将其附加到列表的更有效方法 - Python: More efficient way to break down string and append it to a list 从python列表中剥离HTML标记的最佳方法是什么? - What is the best way to strip HTML tags from a python list? 从 python 中的字典列表中提取子字典的更有效方法是什么? - What is a more efficient way to extract sub-dicts from a list of dicts in python? 在 Python 中迭代列表并找到合适的字符串模式的最快(最有效)方法是什么? - What's the fastest (most efficient) way to iterate through a list and find a fitting string pattern in Python? 在python中将列表拆分为多个列表的有效方法是什么 - What's the efficient way to split a List to several list in python 在Python中以字符串格式对日期列表进行排序的最有效方法是什么? - What is the most efficient way to sort a list of dates in string format in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM