将 Python 列表拆分为重叠块的列表 [英] Splitting a Python list into a list of overlapping chunks
问题描述
这个问题类似于切片列表进入子列表列表,但在我的情况下,我想包含每个前一个子列表的最后一个元素,作为下一个子列表中的第一个元素.并且必须考虑到最后一个元素必须始终至少有两个元素.
例如:
list_ = ['a','b','c','d','e','f','g','h']
大小为 3 的子列表的结果:
resultant_list = [['a','b','c'],['c','d','e'],['e','f','g'],['g','h']]
您链接的答案中的列表推导式是通过简单地缩短传递给范围的step"参数,很容易适应支持重叠块:
<预><代码>>>>list_ = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']>>>n = 3 # 组大小>>>m = 1 #重叠大小>>>[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定义块(可迭代,块大小= 3,重叠= 0):# 我们将使用双端队列来保存值,因为它会自动# 如果它变得太大,则丢弃任何无关元素如果 chunk_size <1:引发异常(块大小太小")如果重叠 >= chunk_size:引发异常(重叠太大")队列 = 双端队列(最大长度 = 块大小)it = iter(可迭代)我 = 0尝试:# 首先用第一组填充队列对于我在范围内(chunk_size):queue.append(next(it))为真:产量元组(队列)# 在产生一个块后,为下一个块获取足够的元素对于 i 在范围内(chunk_size - 重叠):queue.append(next(it))除了停止迭代:# 如果迭代器耗尽,则产生任何剩余的元素我 += 重叠如果我>0:产量元组(队列)[-i:]
注意:我已经在 wimpy.util.chunks
.如果您不介意添加依赖项,您可以 pip install wimpy
并使用 from wimpy import chunks
而不是复制粘贴代码.
This question is similar to Slicing a list into a list of sub-lists, but in my case I want to include the last element of the each previous sub-list, as the first element in the next sub-list. And have to take into account that the last element have always to have at least two elements.
For example:
list_ = ['a','b','c','d','e','f','g','h']
The result for a size 3 sub-list:
resultant_list = [['a','b','c'],['c','d','e'],['e','f','g'],['g','h']]
The list comprehension in the answer you linked is easily adapted to support overlapping chunks by simply shortening the "step" parameter passed to the range:
>>> 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']]
Other visitors to this question mightn't have the luxury of working with an input list (slicable, known length, finite). Here is a generator-based solution that can work with arbitrary iterables:
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:]
Note: I've since released this implementation in wimpy.util.chunks
. If you don't mind adding the dependency, you can pip install wimpy
and use from wimpy import chunks
rather than copy-pasting the code.
这篇关于将 Python 列表拆分为重叠块的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!