我有一个字符串和一个列表:
src = 'ways to learn are read and execute.'
temp = ['ways to','are','and']
我想要的是使用列表temp
的值分割字符串并产生:
['learn','read','execute']
同时。
我尝试过for
循环:
for x in temp:
src.split(x)
这是它产生的:
['','to learn are read and execute.']
['ways to learn','read and execute.']
['ways to learn are read','execute.']
我想要的是首先输出列表中的所有值,然后使用它分割字符串。
有人有解决方案吗?
答案 0 :(得分:7)
re.split
是在多个分隔符上进行拆分的常规解决方案:
import re
src = 'ways to learn are read and execute.'
temp = ['ways to','are','and']
pattern = "|".join(re.escape(item) for item in temp)
result = re.split(pattern, src)
print(result)
结果:
['', ' learn ', ' read ', ' execute.']
您还可以过滤掉空白项目,并通过简单的列表理解来去除空格和标点符号:
result = [item.strip(" .") for item in result if item]
print(result)
结果:
['learn', 'read', 'execute']
答案 1 :(得分:0)
这是一种纯pythonic的方法,不依赖于正则表达式。更冗长,更复杂:
result = []
current = 0
for part in temp:
too_long_result = src.split(part)[1]
if current + 1 < len(temp): result.append(too_long_result.split(temp[current+1])[0].lstrip().rstrip())
else: result.append(too_long_result.lstrip().rstrip())
current += 1
print(result)
如果您不想删除列表条目中的尾部和前导空格,则无法删除.lstrip().rstrip()
命令。
答案 2 :(得分:0)
循环解决方案。您可以根据需要添加条件,例如剥离。
src = 'ways to learn are read and execute.'
temp = ['ways to','are','and']
copy_src = src
result = []
for x in temp:
left, right = copy_src.split(x)
if left:
result.append(left) #or left.strip()
copy_src = right
result.append(copy_src) #or copy_src.strip()
答案 3 :(得分:0)
只需保持简单
src = 'ways to learn are read and execute.'
temp = ['ways','to','are','and']
res=''
for w1 in src.split():
if w1 not in temp:
if w1 not in res.split():
res=res+w1+" "
print(res)