2

我有一个清单如下:

['MARK_A', 8, 7702.5, 13, 7703, 983472],
['MARK_B', 10, 7702.5, 983472],
['MARK_B', 3, 7703.5, 983472],
['MARK_B', 6, 7701.2, 983472],
['MARK_B', 5, 7704.4, 983472],
['MARK_A', 9, 7701.5, 11, 7704, 983475],
['MARK_B', 10, 7702.5, 983475],
['MARK_B', 3, 7703.5, 983475],
['MARK_B', 6, 7701.2, 983475],
['MARK_B', 5, 7704.4, 983475]]

如何将此列表拆分为 2 个列表,如下所示:

[['MARK_A', 8, 7702.5, 13, 7703, 983472],
['MARK_B', 10, 7702.5, 983472],
['MARK_B', 3, 7703.5, 983472],
['MARK_B', 6, 7701.2, 983472],
['MARK_B', 5, 7704.4, 983472]],
[ ['MARK_A', 9, 7701.5, 11, 7704, 983475],
['MARK_B', 10, 7702.5, 983475],
['MARK_B', 3, 7703.5, 983475],
['MARK_B', 6, 7701.2, 983475],
['MARK_B', 5, 7704.4, 983475]]

列表中可以有任意数量的“MARK_A”,后跟一个或多个“MARK_B”。我将列表除以 [-1] 元素

4

1 回答 1

3

itertools.groupby用于这类问题(得到一个集合集合,用内部集合中的标记拆分它)

from itertools import groupby

class GroupbyHelper:
    def __init__(self, token):
        self.token = token
        self.count = 0

    def __call__(self, item):
        self.count += (item[0] == self.token)
        return self.count


grouped_collections = \
[list(grouped) for _, grouped in groupby(collections, GroupbyHelper("MARK_A"))]

上面代码的一些通用版本:

from itertools import groupby

class GroupbyHelper:
    def __init__(self, check_token):
        self.check_token = check_token
        self.count = 0

    def __call__(self, item):
        self.count += self.check_token(item)
        return self.count


grouped_collections = \
[list(grouped) for _, grouped in 
 groupby(collections, GroupbyHelper(lambda x: x[0] == "MARK_A"))]

使用两个迭代器:

from itertools import tee, zip_longest

iter1, iter2 = tee(i for i, item in enumerate(collections) if item[0] == 'MARK_A')
next(iter2) #advance the second iterator so we can move by range

grouped_collections = \
[collections[s:e] for s, e in zip_longest(iter1, iter2, fillvalue=len(collections))]

有时一个简单的 for 循环并不是那么糟糕:

grouped_collections = []
for lst in collections:
    if lst[0]=="MARK_A":
        grouped_collections.append([lst])
    else:
        grouped_collections[-1].append(lst)
于 2013-04-17T05:41:09.323 回答