繁体   English   中英

同时使用值列表拆分字符串

[英]Split a string using a list of value at the same time

我有一个字符串和一个列表:

src = 'ways to learn are read and execute.'
temp = ['ways to','are','and']

我想要的是使用list 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.']

我想要的是首先输出列表中的所有值,然后使用它拆分字符串。

有人有解决方案吗?

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']

这是一种纯粹的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()命令。

循环解决方案 如果需要,可以添加条带等条件。

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()

保持简单

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM