2017-07-26 62 views
1

給定一個字符串:根據Python中的搜索詞對字符串進行分區?

x = 'foo test1 test1 foo test2 foo' 

我想foo分割字符串,所以我得到的線沿線的東西:

['foo', 'test1 test1 foo', 'test2 foo'] (preferred) 

       or 

[['foo'], ['test1', 'test1', 'foo'], ['test2', 'foo']] (not preferred, but workable) 

我試過itertools.groupby

In [1209]: [list(v) for _, v in itertools.groupby(x.split(), lambda k: k != 'foo')] 
Out[1209]: [['foo'], ['test1', 'test1'], ['foo'], ['test2'], ['foo']] 

但它並不完全給我我要找的東西。我知道我可以使用一個循環,這樣做:

In [1210]: l = [[]] 
     ...: for v in x.split(): 
     ...:  l[-1].append(v) 
     ...:  if v == 'foo': 
     ...:   l.append([]) 
     ...:  

In [1211]: l 
Out[1211]: [['foo'], ['test1', 'test1', 'foo'], ['test2', 'foo'], []] 

但它不是非常有效的離開空列表的末尾。有一種更簡單的方法嗎?

我想保留分隔符。

+1

可以通過Python中的分隔符[分割字符串複製](https://stackoverflow.com/questions/3475251/split-a-string-by-a-delimiter-in-python) –

+1

如果字符串不以''foo''結尾會出現什麼情況? – holdenweb

+0

@holdenweb它總是會的。 –

回答

3

也許不是最漂亮的方法,但簡潔straightfoward:

[part + 'foo' for part in g.split('foo')][:-1] 

輸出:

['foo', ' test1 test1 foo', ' test2 foo'] 
+0

不錯。沒想到這個。 –

3

您可以使用str.partition爲您的情況:

def find_foo(x): 
    result = [] 
    while x: 
     before, _, x = x.partition("foo") 
     result.append(before + "foo") 
    return result 

>>> find_foo('foo test1 test1 foo test2 foo') 
>>> ['foo', ' test1 test1 foo', ' test2 foo'] 
0

試試這個

x = 'foo test1 test1 foo test2 foo' 

word = 'foo' 
out = [] 
while word in x: 
    pos = x.index(word) 
    l = len(word) 
    out.append(x[:int(pos)+l]) 
    x = x[int(pos)+l:] 

print out 

輸出:

['foo', ' test1 test1 foo', ' test2 foo'] 
1

假如你想過遍歷字符串,並使用您的搜索起始位置?這通常會比您隨時切斷絃樂更快。這可能會爲你工作:

x = 'foo test1 test1 foo test2 foo' 

def findall(target, s): 
    lt =len(target) 
    ls = len(s) 
    pos = 0 
    result = [] 
    while pos < ls: 
     fpos = s.find(target, pos)+lt 
     result.append(s[pos:fpos]) 
     pos = fpos 
    return result 

print(findall("foo", x)) 
1

你可以使用背後積極(?<=)正則表達式的樣子

In [515]: string = 'foo test1 test1 foo test2 foo' 

In [516]: re.split('(?<=foo)\s', string) 
Out[516]: ['foo', 'test1 test1 foo', 'test2 foo'] 

而且,

In [517]: [x.split() for x in re.split('(?<=foo)\s', string)] 
Out[517]: [['foo'], ['test1', 'test1', 'foo'], ['test2', 'foo']] 
相關問題