python - 将 Python 列表拆分为重叠 block 列表

标签 python

这个问题类似于 Slicing a list into a list of sub-lists ,但在我的例子中,我想将每个先前子列表的最后一个元素包含为下一个子列表中的第一个元素。而且我必须考虑到最后一个子列表必须至少有两个元素。

例如:

list_ = ['a','b','c','d','e','f','g','h']

大小为 3 的子列表的结果:

resultant_list = [['a','b','c'],['c','d','e'],['e','f','g'],['g','h']]

最佳答案

answer you linked 中的列表理解通过简单地缩短传递给范围的“步骤”参数,很容易适应支持重叠 block :

>>> list_ = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
>>> n = 3  # group size
>>> m = 1  # overlap size
>>> [list_[i:i+n] for i in range(0, len(list_), n-m)]
[['a', 'b', 'c'], ['c', 'd', 'e'], ['e', 'f', 'g'], ['g', 'h']]

这个问题的其他访问者可能没有使用输入列表(可切片、已知长度、有限)的奢侈。这是一个基于生成器的解决方案,可以处理任意可迭代对象:

from collections import deque

def chunks(iterable, chunk_size=3, overlap=0):
    # we'll use a deque to hold the values because it automatically
    # discards any extraneous elements if it grows too large
    if chunk_size < 1:
        raise Exception("chunk size too small")
    if overlap >= chunk_size:
        raise Exception("overlap too large")
    queue = deque(maxlen=chunk_size)
    it = iter(iterable)
    i = 0
    try:
        # start by filling the queue with the first group
        for i in range(chunk_size):
            queue.append(next(it))
        while True:
            yield tuple(queue)
            # after yielding a chunk, get enough elements for the next chunk
            for i in range(chunk_size - overlap):
                queue.append(next(it))
    except StopIteration:
        # if the iterator is exhausted, yield any remaining elements
        i += overlap
        if i > 0:
            yield tuple(queue)[-i:]

注意:我已经在 wimpy.util.chunks 中发布了这个实现.如果您不介意添加依赖项,您可以pip install wimpy 并使用from wimpy import chunks 而不是复制粘贴代码。

关于python - 将 Python 列表拆分为重叠 block 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36586897/

相关文章:

python - 从单元格中检索完整的富文本数据(单元格内有多种字体颜色/样式)

python - 如果使用python在捕获区域中存在某些颜色,如何返回 bool 值

python - GAE 搜索 API : custom snippet length

python - 修复交互式图表中的轴比例

python - 如何使用列表理解来存储列表的嵌套列表

python - Kivy 相机作为 KV 语言小部件

python - 内容处置不起作用

python - python中的随机样本

python - 如果它不以 http 开头,我如何将 http 添加到 url?

python - 是否有可用于额外参数的 pip 环境变量?