2012-06-01 26 views
10

我想通過索引列表拆分字符串,其中拆分段以一個索引開始,並在下一個索引之前結束。通過索引列表拆分字符串

例子:

s = 'long string that I want to split up' 
indices = [0,5,12,17] 
parts = [s[index:] for index in indices] 
for part in parts: 
    print part 

這將返回:

長字符串,我想分手了
字符串,我想分手了
,我想拆分
我想分手

我試圖讓:




我想分手

回答

15
s = 'long string that I want to split up' 
indices = [0,5,12,17] 
parts = [s[i:j] for i,j in zip(indices, indices[1:]+[None])] 

回報

['long ', 'string ', 'that ', 'I want to split up'] 

您可以打印使用:

print '\n'.join(parts) 

另一種可能性(但不復制indices)將是:

s = 'long string that I want to split up' 
indices = [0,5,12,17] 
indices.append(None) 
parts = [s[indices[i]:indices[i+1]] for i in xrange(len(indices)-1)] 
+1

另一種方式是,'[s [i:j] for i,j in izip_longest(indices,indices [1:])]''但我更喜歡你的方式! – jamylak

+0

這將索引列表複製到索引[1:]中,並通過'zip'函數創建一個具有雙倍大小的新列表 - >性能和內存消耗不佳。 – schlamar

+2

@ ms4py這很好,在這種情況下性能不是問題,這是一個非常可讀的解決方案。如果表現是一個問題,我的建議可以使用。 – jamylak

3

這裏是與itertools module的大量使用很短的解決方案。 tee函數用於在索引上成對地迭代。請參閱模塊配方以獲取更多幫助。

>>> from itertools import tee, izip_longest 
>>> s = 'long string that I want to split up' 
>>> indices = [0,5,12,17] 
>>> start, end = tee(indices) 
>>> end.next() 
0 
>>> [s[i:j] for i,j in izip_longest(start, end)] 
['long ', 'string ', 'that ', 'I want to split up'] 

編輯:這是不會複製的索引列表,所以它應該是更快的一個版本。

+0

感謝alt方法 - 我必須在某時檢查itertools – Yarin

+0

整潔的方法,學到了新的東西。有沒有簡單的方法來擺脫表達式中前3個字符串末尾的額外空白?我嘗試過'[i:j] .strip()',但根本不起作用(不知道爲什麼) – Levon

+0

如果你要使用它,你可以直接使用itertools文檔中的pairwise函數。爲了兼容python 3,使用'next(end)'優先於'end.next()'。 – jamylak