我正在嘗試創建一個函式來決議基于多個分隔符的字串,但采用分層格式:即嘗試第一個分隔符,然后是第二個,然后是第三個,等等。
這個問題似乎提供了一個解決方案,專門鏈接這個評論。
# Split the team names, with a hierarchical delimiter
def split_new(inp, delims=['VS', '/ ' ,'/']):
# https://stackoverflow.com/questions/67574893/python-split-string-by-multiple-delimiters-following-a-hierarchy
for d in delims:
result = inp.split(d, maxsplit=1)
if len(result) == 2:
return result
else:
return [inp] # If nothing worked, return the input
test_strs = ['STACK/ OVERFLOW', 'STACK #11/00 VS OVERFLOW', 'STACK/OVERFLOW' ]
for ts in test_strs:
res = split_new(ts)
print(res)
"""
Output:
['STACK/ OVERFLOW']
['STACK #11/00 ', ' OVERFLOW']
['STACK/OVERFLOW']
Expected:
['STACK',' OVERFLOW']
['STACK #11/00 ', ' OVERFLOW']
['STACK', 'OVERFLOW']
"""
但是,我的結果并不像預期的那樣。我錯過了什么?
uj5u.com熱心網友回復:
在嘗試所有分隔符后執行“沒有任何效果”后備:
for d in delims:
result = inp.split(d, maxsplit=1)
if len(result) == 2:
return result
return [inp] # If nothing worked, return the input
uj5u.com熱心網友回復:
作為替代方案,您可以使用帶有交替的單個模式,而不是回圈分隔符|
import re
test_strs = ['STACK/ OVERFLOW', 'STACK #11/00 VS OVERFLOW', 'STACK/OVERFLOW' ]
pattern = r"/(?!\d)|VS"
for s in test_strs:
print(re.split(pattern, s))
輸出
['STACK', ' OVERFLOW']
['STACK #11/00 ', ' OVERFLOW']
['STACK', 'OVERFLOW']
uj5u.com熱心網友回復:
這是因為您嘗試在回圈的第一次迭代中回傳結果,當“VS”被拆分時,您使用 else 陳述句回傳結果
正確的做法是:
def split_new(inp, delims=['VS', '/ ' ,'/']):
for d in delims:
result = inp.split(d, maxsplit=1)
if len(result) == 2:
return result
return [inp] # If nothing worked, return the input
test_strs = ['STACK/ OVERFLOW', 'STACK #11/00 VS OVERFLOW', 'STACK/OVERFLOW' ]
for ts in test_strs:
res = split_new(ts)
print(res)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/527835.html
