将列表拆分为由分隔符确定的块

Split a list into chunks determined by a separator

我有这个列表 (python):

[[item1],[item2],[item3],[/],[item4],[item5],[item6],[/]...]

我想将它们分成块,将进入每个块的元素是分隔符“/”之前的元素。

所以我的块看起来像:

chunk1 = [[item1],[item2],[item3]]
chunk2 = [[item4],[item5],[item6]]

我试了又试,没有想出有效的方法。尝试使用 for 和 if element[x] == '/' 循环遍历它,然后获得一些位置。它很脏而且不能正常工作。

如有任何帮助,我们将不胜感激。

收集连续块的常用方法是使用itertools.groupby,例如:

>>> from itertools import groupby
>>> blist = ['item1', 'item2', 'item3', '/', 'item4', 'item5', 'item6', '/']
>>> chunks = (list(g) for k,g in groupby(blist, key=lambda x: x != '/') if k)
>>> for chunk in chunks:
...     print(chunk)
...     
['item1', 'item2', 'item3']
['item4', 'item5', 'item6']

(您对列表的表示 [item1],[item2],[item3],[/], 使它看起来像列表中的每个元素实际上都是一个列表,在这种情况下,相同的方法将起作用,您只需要与 ['/'] 或任何你的分隔符。)

我写了一些更简单的东西让你理解 - 基本上寻找 '/',如果它不存在,请继续附加到块中。 itertools.groupby 值得学习,但是先了解一些更简单的东西是一个好主意。

l = ['i1', 'i2', 'i3', '/', 'i4', 'i5', 'i6', '/']

chunks = []
x = 0
chunks.append([])   # create an empty chunk to which we'd append in the loop
for i in l:
    if i != '/':
        chunks[x].append(i)
    else:
        x += 1
        chunks.append([])

print chunks

如果你的元素是字符串,有一个更快的方法来完成我在 python 中所做的事情 - 基本上 - 首先创建一个 ' ' (space) 分隔的字符串,然后,首先按 '/' 拆分,然后再按 ' ' 拆分。

l = ['i1', 'i2', 'i3', '/', 'i4', 'i5', 'i6', '/']

s = " ".join(l)  # first create a string, joining by a <space> it could be anything

chunks2 = [x.split() for x in s.split("/")]
print chunks2

也可以这样做(假设不需要空块并且 l 是列表 "chunked"):

chunks, last_chunk = [], []
for x in l:
    if x == '/':
         if last_chunk:
             chunks.append(last_chunk)
             last_chunk = []
    else:
         last_chunk.append(x)
if last_chunk:
    chunks.append(last_chunk)

不如 groupby 解决方案灵活,但万一有人要使用 Numpy 数组,并且只有一个(或固定的少量)分隔符:

i, = np.where(array_of_str=='/')[0]
bulk1, bulk2 = array_of_str[:i], array_of_str[i+1:]