SIice 基于间隔的 numpy 数组?
SIice a numpy array based on an interval?
是否可以在numpy中将长度为m的一维数组按间隔n系统地切片?假设我有一个包含 1000 个值的列表,我可以轻松地将其分成 10 个包含 100 个值的列表吗?
您可以同时使用 np.array_split()
和 np.split()
,它们实际上是相同的,但需要注意一点(根据 np.array_split()
)
来自文档:
x = np.arange(8.0)
np.array_split(x, 3)
#Result
[array([0., 1., 2.]), array([3., 4., 5.]), array([6., 7.])]
Split an array into multiple sub-arrays.
Please refer to the split documentation. The only difference between
these functions is that array_split allows indices_or_sections to be
an integer that does not equally divide the axis. For an array of
length l that should be split into n sections, it returns l % n
sub-arrays of size l//n + 1 and the rest of size l//n.
array_split 也允许不等间距拆分,如果这能满足您的需求
ar = np.arange(0, 20, dtype='int')
s = [2, 7, 12, 17]
np.array_split(ar, s)
Out[80]:
[array([0, 1]),
array([2, 3, 4, 5, 6]),
array([ 7, 8, 9, 10, 11]),
array([12, 13, 14, 15, 16]),
array([17, 18, 19])]
是否可以在numpy中将长度为m的一维数组按间隔n系统地切片?假设我有一个包含 1000 个值的列表,我可以轻松地将其分成 10 个包含 100 个值的列表吗?
您可以同时使用 np.array_split()
和 np.split()
,它们实际上是相同的,但需要注意一点(根据 np.array_split()
)
来自文档:
x = np.arange(8.0)
np.array_split(x, 3)
#Result
[array([0., 1., 2.]), array([3., 4., 5.]), array([6., 7.])]
Split an array into multiple sub-arrays.
Please refer to the split documentation. The only difference between these functions is that array_split allows indices_or_sections to be an integer that does not equally divide the axis. For an array of length l that should be split into n sections, it returns l % n sub-arrays of size l//n + 1 and the rest of size l//n.
array_split 也允许不等间距拆分,如果这能满足您的需求
ar = np.arange(0, 20, dtype='int')
s = [2, 7, 12, 17]
np.array_split(ar, s)
Out[80]:
[array([0, 1]),
array([2, 3, 4, 5, 6]),
array([ 7, 8, 9, 10, 11]),
array([12, 13, 14, 15, 16]),
array([17, 18, 19])]