Consecutive, Overlapping Subsets of Array (NumPy, Python)
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like [[1,2,3,4], [2,3,4,5], [3,4,5,6], ..., [11,12,13,14]] . Sure this is possible by looping over the large array and adding arrays of length four to the new array, but I'm curious if there is some secret 'magic' Python method doing just this :) 回答1: The fastest way seems to be to preallocate the array, given as option 7 right at the bottom of this answer. >>> import numpy as np >>> A=np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14]) >>> A array([ 1, 2,