说我有一堆字符串,可以以pineapple
,ham
或grapefruit
结尾。生成“剥离字符串”列表的最佳方法是什么,它与原始字符串完全相同,除非字符串以pineapple
结尾,pineapple
将从末尾剪裁,并且如果字符串以ham
结尾,ham
会被修剪吗?
例如,假设我的输入是
["I really like pineapple",
"I don't like ham or grapefruit",
"Today I ate a lot of ham",
"but I also ate a lot of grapefruit"]
然后输出应为
["I really like",
"I don't like ham or grapefruit",
"Today I ate a lot of",
"but I also ate a lot of grapefruit"]
答案 0 :(得分:2)
似乎是re
的工作。只需在表达式的末尾添加一个行尾锚点,使其仅在末尾处为子。
import re
stuff = ["I really like pineapple", "I don't like ham or grapefruit", "Today I ate a lot of ham", "but I also ate a lot of grapefruit"]
[re.sub(r'(pineapple|ham)$','',s).strip() for s in stuff]
Out[7]:
['I really like',
"I don't like ham or grapefruit",
'Today I ate a lot of',
'but I also ate a lot of grapefruit']
(我添加了额外的strip()
来清理任何剩余的空格)
要使用任意字符串列表执行此操作,它将如下所示:
re.sub(r'({})$'.format('|'.join(arbitrary_list_of_strings)),'',s)...
答案 1 :(得分:1)
In [3]: L = ["I really like pineapple",
...: "I don't like ham or grapefruit",
...: "Today I ate a lot of ham",
...: "but I also ate a lot of grapefruit"]
In [4]: suffixes = 'pineapple ham grapefruit'.split()
In [5]: for i in range(len(L)):
...: while any(L[i].endswith(suffix) for suffix in suffixes):
...: for suffix in suffixes:
...: if L[i].endswith(suffix):
...: L[i] = L[i][:-len(suffix)]
...: break
...:
In [6]: L
Out[6]:
['I really like ',
"I don't like ham or ",
'Today I ate a lot of ',
'but I also ate a lot of ']
如果您想清理那些额外的空白,可以这样做:
In [7]: L = ["I really like pineapple",
"I don't like ham or grapefruit",
"Today I ate a lot of ham",
"but I also ate a lot of grapefruit"]
In [8]: for i in range(len(L)):
while any(L[i].endswith(suffix) for suffix in suffixes):
for suffix in suffixes:
if L[i].endswith(suffix):
L[i] = L[i][:-len(suffix)-1]
break
...:
In [9]: L
Out[9]:
['I really like',
"I don't like ham or",
'Today I ate a lot of',
'but I also ate a lot of']
请注意,这也会杀死目标后缀的任何重复出现:
In [10]: L = ["I really like pineapple pineapple",
"I don't like ham or grapefruit",
"Today I ate a lot of ham",
"but I also ate a lot of grapefruit"]
In [11]: for i in range(len(L)):
while any(L[i].endswith(suffix) for suffix in suffixes):
for suffix in suffixes:
if L[i].endswith(suffix):
L[i] = L[i][:-len(suffix)-1]
break
....:
In [12]: L
Out[12]:
['I really like',
"I don't like ham or",
'Today I ate a lot of',
'but I also ate a lot of']