我需要编写一个Python函数,当传递一个数组和一个整数N时,该函数返回该数组的内容,该内容分为相等大小的N个子数组。
如果无法将数组的长度除以N,则最终的子数组必须具有合适的长度以容纳其余元素。
示例:
split_array(array=[1,2,3,4,5,6,7,8,9,10],n=4)
应输出:[[1,3],[4,6],[7,8],[9,10]]
我的研究表明numpy.array_split函数确实做到了这一点,我查看了GitHub上的源代码,发现它首先组成了一个包含所有子数组大小的数组,然后它迭代以拆分原始数组。
来自numpy.array_split的删节样本
def array_split(ary,indices_or_sections,axis=0):
# indices_or_sections is a scalar,not an array.
Nsections = int(indices_or_sections)
if Nsections <= 0:
raise ValueError('number sections must be larger than 0.')
Neach_section,extras = divmod(Ntotal,Nsections)
section_sizes = ([0] +
extras * [Neach_section+1] +
(Nsections-extras) * [Neach_section])
div_points = _nx.array(section_sizes,dtype=_nx.intp).cumsum()
sub_arys = []
sary = _nx.swapaxes(ary,axis,0)
for i in range(Nsections):
st = div_points[i]
end = div_points[i + 1]
sub_arys.append(_nx.swapaxes(sary[st:end],0))
return sub_arys
我唯一要理解的是变量section_sizes
是如何数学创建的。
对于示例split_array(array=[1,n=4)
,它构建了一个[3,2]
大小列表,这正是我需要的大小,但是我不明白为什么它起作用。
我知道divmod(Ntotal,Nsections)
将为您提供除法计算的商(Neach_section
)和余数(extras
)。
但是为什么quotient * [remainder+1]
总是给您正确数量的正确大小的“商”子数组大小(在此示例[3,3]中)?
为什么[quotient-remainder] * quotient
为您提供正确数量的正确大小的“剩余”子数组大小(在本示例中为[2,2])?
有人甚至可以告诉我这种运算是什么,或者它涉及的是数学的哪个分支,因为这不是我以前遇到的。