python numpy将数组拆分为不相等的子数组 [英] python numpy split array into unequal subarrays

查看:392
本文介绍了python numpy将数组拆分为不相等的子数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数组拆分为n个部分.有时这些部分的大小相同,有时它们的大小不同.

I am trying to split an array into n parts. Sometimes these parts are of the same size, sometimes they are of a different size.

我正在尝试使用:

split = np.split(list, size)

当大小均分为列表时,此方法工作正常,但否则失败.有没有一种方法可以用额外的很少"元素来填充"最终的数组?

This works fine when size divides equally into the list, but fails otherwise. Is there a way to do this which will 'pad' the final array with the extra 'few' elements?

推荐答案

def split_padded(a,n):
    padding = (-len(a))%n
    return np.split(np.concatenate((a,np.zeros(padding))),n)

这篇关于python numpy将数组拆分为不相等的子数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆