2013-03-12 47 views
11

我有一個包含各種字符串值的列表。每當我看到WORD時,我想分割列表。結果將是列表(這將是原始列表的子列表),其中包含WORD的一個實例,我可以使用循環做到這一點,但有沒有更pythonic方法來做到這一點?Python基於分隔符詞劃分列表

示例= ['A', 'WORD', 'B' , 'C' , 'WORD' , 'D']

結果= [['A'], ['WORD','B','C'],['WORD','D']]

這是我試過,但它實際上並沒有達到我想要的,因爲它會把WORD在不同的列表,它應該是:

def split_excel_cells(delimiter, cell_data): 

    result = [] 

    temp = [] 

    for cell in cell_data: 
     if cell == delimiter: 
      temp.append(cell) 
      result.append(temp) 
      temp = [] 
     else: 
      temp.append(cell) 

    return result 

回答

10

我會用發電機:

def group(seq, sep): 
    g = [] 
    for el in seq: 
     if el == sep: 
      yield g 
      g = [] 
     g.append(el) 
    yield g 

ex = ['A', 'WORD', 'B' , 'C' , 'WORD' , 'D'] 
result = list(group(ex, 'WORD')) 
print(result) 

這將打印

[['A'], ['WORD', 'B', 'C'], ['WORD', 'D']] 

代碼接受任何可迭代,併產生一個可迭代(你不拼合到一個列表,如果你不希望)。

1

@ NPE的解決方案對我來說看起來很pythonic。這是另外一個使用itertools

from itertools import izip, chain 
example = ['A', 'WORD', 'B' , 'C' , 'WORD' , 'D'] 
indices = [i for i,x in enumerate(example) if x=="WORD"] 
pairs = izip(chain([0], indices), chain(indices, [None])) 
result = [example[i:j] for i, j in pairs] 

該代碼主要是基於this answer

+0

謝謝我也試圖根據指數進行分割,但不知道如何配對。這是一個非常好的方法。 – Cemre 2013-03-12 11:00:24

10
import itertools 

lst = ['A', 'WORD', 'B' , 'C' , 'WORD' , 'D'] 
w = 'WORD' 

spl = [list(y) for x, y in itertools.groupby(lst, lambda z: z == w) if not x] 

這將創建無定界符分裂名單,這看起來更合乎邏輯的對我說:

[['A'], ['B', 'C'], ['D']] 

如果你堅持的分隔符被包括在內,這應該做的伎倆:

spl = [[]] 
for x, y in itertools.groupby(lst, lambda z: z == w): 
    if x: spl.append([]) 
    spl[-1].extend(y) 
+1

這是'itertools'的一個很好的用法。 – 2016-04-19 10:33:00

+1

強烈建議使用這個答案,因爲它與內建的'itertools'模塊有很多pythonic! – Drake 2016-10-10 08:01:38

+0

不幸的是,如果重複了分隔符,第二個版本會給出錯誤的結果。 – 2017-10-08 12:18:19