python-3.x 如何根据索引子列表对元素列表进行分组或批处理?

tct7dpnv  于 2023-05-30  发布在  Python
关注(0)|答案(2)|浏览(142)

如何在groupbatch元素列表中基于索引子列表?以下是应考虑的可能情况。希望能帮到你。

第一个示例:elements的长度==所有indices元素的总和

indices = [1, 3, 5]
elements = ['A','B','C','D','E','F','G','H','I']

Output:
[['A'], ['B','C','D'],['E','F','G','H','I']]

第二个示例:elements的长度>所有indices元素的总和

应考虑indices的最大元素,然后相应地对elements列表进行分组

indices = [1, 3, 5]
elements = ['A','B','C','D','E','F','G','H','I', 'J','K','L','M','N']

Output:
[['A'], ['B','C','D'],['E','F','G','H','I'],['J','K','L','M','N']]

第三个示例:elements的长度>所有indices元素的总和

应考虑indices的最大元素,然后相应地对elements列表进行分组

indices = [1, 3, 5]
elements = ['A','B','C','D','E','F','G','H','I', 'J','K','L']

Output:
[['A'], ['B','C','D'],['E','F','G','H','I'],['J','K','L']]

第四个示例:elements的长度>所有indices元素的总和

应考虑indices的最大元素,然后相应地对elements列表进行分组

indices = [1, 3, 5]
elements = ['A','B','C','D','E','F','G','H','I', 'J','K','L','M','N','O','P','Q','R','S']

Output:
[['A'], ['B','C','D'],['E','F','G','H','I'],['J','K','L','M','N'], ['O','P','Q','R','S']]

第5个示例:elements的长度<所有indices元素的总和

indices = [1, 3, 5]
elements = ['A','B','C']

Output:
[['A'], ['B','C']]

第6个示例:elements的长度<所有indices元素的总和

indices = [1, 3, 5]
elements = ['A','B','C','D','E','F']

Output:
[['A'], ['B','C','D'],['E','F']]

如何在Python中实现这一点?

t2a7ltrp

t2a7ltrp1#

可以使用itertools.isliceelements进行切片:

from itertools import islice

indices = [1, 3, 5]
elements = ['A','B','C','D','E','F','G','H','I', 'J','K','L','M','N','O','P','Q','R','S']

def get_cnt(indices):
    if len(indices) == 0:
        return
    yield from indices
    while True:
        yield indices[-1]

out, it, g = [], iter(elements), get_cnt(indices)
while slice_:=list(islice(it, next(g))):
    out.append(slice_)

print(out)

图纸:

[['A'], ['B', 'C', 'D'], ['E', 'F', 'G', 'H', 'I'], ['J', 'K', 'L', 'M', 'N'], ['O', 'P', 'Q', 'R', 'S']]
qc6wkl3g

qc6wkl3g2#

不使用itertools

indices = [1, 3, 5]

def groupWith(elements, indices):
    if sum(indices)<len(elements):
        q, r = divmod(len(elements)-sum(indices), indices[-1])
        indices += [indices[-1]] * q + ([r] if r>0 else [])
    elif sum(indices)>len(elements):
        cumsum_indices = [sum(indices[:i+1]) for i in range(len(indices))]
        for j, e in enumerate(cumsum_indices):
            if e>len(elements):
                indices = indices[:j] + [len(elements)-sum(indices[:j])]
                break
    ret = []
    for idx, elem in enumerate(indices):
        start = int(idx>0) * sum(indices[:idx])
        ret += [elements[start:start+elem]]
    return ret
    
print(groupWith(['A','B','C','D','E','F','G','H','I'], indices))
print(groupWith(['A','B','C','D','E','F','G','H','I', 'J','K','L','M','N'], indices))
print(groupWith(['A','B','C','D','E','F','G','H','I', 'J','K','L'], indices))
print(groupWith(['A','B','C','D','E','F','G','H','I', 'J','K','L','M','N','O','P','Q','R','S'], indices))
print(groupWith(['A','B','C'], indices))
print(groupWith(['A','B','C','D','E','F'], indices))

输出:

[['A'], ['B', 'C', 'D'], ['E', 'F', 'G', 'H', 'I']]
[['A'], ['B', 'C', 'D'], ['E', 'F', 'G', 'H', 'I'], ['J', 'K', 'L', 'M', 'N']]
[['A'], ['B', 'C', 'D'], ['E', 'F', 'G', 'H', 'I'], ['J', 'K', 'L']]
[['A'], ['B', 'C', 'D'], ['E', 'F', 'G', 'H', 'I'], ['J', 'K', 'L', 'M', 'N'], ['O', 'P', 'Q', 'R', 'S']]
[['A'], ['B', 'C']]
[['A'], ['B', 'C', 'D'], ['E', 'F']]

**注意:**这有点粗糙,可以很好地进行优化。

相关问题