通过每次移动启动索引,将数组分为多个小阵列
如何通过每次将数组启动位置转移一个小数组来得出多个小阵列?
示例输入数组
[1,2,3,4,5,6,7,8,9,10]
输出:大小3的多个子阵列,从i = 0,1,2,3 .....
[1,2,3] [2,3,4] [3,4,5] [4,5,6] [5,6,7] [6,7,8] [7,8,9] [8,9,10]
我可以通过编写自己的代码来做到这一点,
split(input){
i =0
results = []
while(i<len(input)-3):
start = i
end = i+ 3
subarray = input[start:end]
results.append(subarray)
}
return results
是否有python内置函数,或者sum lib?
How to derive multiple small arrays from a large array by shifting array start position by one each time?
Example input array
[1,2,3,4,5,6,7,8,9,10]
Output: Multiple subarrays of size 3, starting at i=0,1,2,3.....
[1,2,3]
[2,3,4]
[3,4,5]
[4,5,6]
[5,6,7]
[6,7,8]
[7,8,9]
[8,9,10]
I can do this by writing my own code
split(input){
i =0
results = []
while(i<len(input)-3):
start = i
end = i+ 3
subarray = input[start:end]
results.append(subarray)
}
return results
Is there a python inbuilt function for this, or sum lib?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不在标准库中,但这是一个很棒的软件包:使用
more_itertools.windowed
:输出:
或使用
numpy
:输出:输出:
Not in the standard library, but this is a great package: using
more_itertools.windowed
:output:
Or with
numpy
:output:
尝试
ZIP
:Try with
zip
:您可以使用列表理解和索引这样的列表来完成此操作:
k是每个子列表中的项目数量。
You can do this with list comprehension and indexing like so:
Where k is the number of items in each sub-list.
您可以通过Numpy做到这一点:
You can do this just by NumPy as:
这是您可以通过列表理解来完成的方式:
Here's how you can do it with list comprehension: