這個小功能應該做的正是你想要的,它也能處理,其中LEN(陣列)是不小數組的長度的整數倍的情況。當然,您可以輸入分割大小和重疊的擴展名。
def split_overlap(array,size,overlap):
result = []
while True:
if len(array) <= size:
result.append(array)
return result
else:
result.append(array[:size])
array = array[size-overlap:]
如何使用它:
array = list(range(10))
print split_overlap(array,4,2)
[[0, 1, 2, 3], [2, 3, 4, 5], [4, 5, 6, 7], [6, 7, 8, 9]]
array = list(range(11))
print split_overlap(array,4,2)
[[0, 1, 2, 3], [2, 3, 4, 5], [4, 5, 6, 7], [6, 7, 8, 9], [8, 9, 10]]
您的具體情況:
A = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
split_overlap(A,7,6)
[[1, 2, 3, 4, 5, 6, 7],
[2, 3, 4, 5, 6, 7, 8],
[3, 4, 5, 6, 7, 8, 9],
[4, 5, 6, 7, 8, 9, 10],
[5, 6, 7, 8, 9, 10, 11],
[6, 7, 8, 9, 10, 11, 12],
[7, 8, 9, 10, 11, 12, 13],
[8, 9, 10, 11, 12, 13, 14],
[9, 10, 11, 12, 13, 14, 15]]
所示的l1,l2 ... ln完成它的第一步是嘗試。 – Julien
:) @Tonechas - 在這裏的所有答案,我應該感謝你,因爲這是適合我的要求最好的一個!不幸的是,我沒有足夠的積分來upvote你的答案..和stackoverflow不允許評論只是感謝一個答案.. – AntonyP