![](/img/trans.png)
[英]How to split a string based on a reference list and on words at the same time efficiently in python?
[英]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.