在没有 for 循环的情况下对数组进行切片
Slicing an array without for loop
我正在尝试将一个大数组划分为较小的子数组,但如果不求助于一种非常幼稚的方法(仅使用两个索引遍历数组),我无法弄清楚该怎么做,如以下代码:
import numpy as np
a = np.arange(100)
idx = np.linspace(0, 100, 9).astype(np.int16)
for idx_start, idx_end in zip(idx[:-1], idx[1:]):
print(a[idx_start:idx_end])
输出:
[ 0 1 2 3 4 5 6 7 8 9 10 11]
[12 13 14 15 16 17 18 19 20 21 22 23 24]
[25 26 27 28 29 30 31 32 33 34 35 36]
[37 38 39 40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59 60 61]
[62 63 64 65 66 67 68 69 70 71 72 73 74]
[75 76 77 78 79 80 81 82 83 84 85 86]
[87 88 89 90 91 92 93 94 95 96 97 98 99]
我看到了 this 答案,但它不符合我的要求,原因有两个:
- 它生成重叠数组
- 它甚至比
for
循环更不直观
我的问题是:
有没有办法跳过循环,并以更“优雅”的方式进行(例如,通过使用一些 numpy
功能等)
提前致谢
尝试一下,不确定这是否有帮助,但在列表理解中使用 for 循环:
>>> import math
>>> new_size=10
使用列表理解创建列表列表:
>>> new_list = [[] for i in range(math.floor(a.size/new_size))]
使用列表理解复制到新列表
>>> [new_list[math.floor(i/new_size)].append(i) for i in a]
[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
结果:
>>> new_list
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]]
>>>
如果您希望结果保留为 numpy 数组
np.array_split(a, 9)
[array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]),
array([12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]),
array([23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]),
array([34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44]),
array([45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55]),
array([56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66]),
array([67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77]),
array([78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88]),
array([89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])]
如果你想要列表作为输出
[i.tolist() for i in np.array_split(a, 9)]
我正在尝试将一个大数组划分为较小的子数组,但如果不求助于一种非常幼稚的方法(仅使用两个索引遍历数组),我无法弄清楚该怎么做,如以下代码:
import numpy as np
a = np.arange(100)
idx = np.linspace(0, 100, 9).astype(np.int16)
for idx_start, idx_end in zip(idx[:-1], idx[1:]):
print(a[idx_start:idx_end])
输出:
[ 0 1 2 3 4 5 6 7 8 9 10 11]
[12 13 14 15 16 17 18 19 20 21 22 23 24]
[25 26 27 28 29 30 31 32 33 34 35 36]
[37 38 39 40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59 60 61]
[62 63 64 65 66 67 68 69 70 71 72 73 74]
[75 76 77 78 79 80 81 82 83 84 85 86]
[87 88 89 90 91 92 93 94 95 96 97 98 99]
我看到了 this 答案,但它不符合我的要求,原因有两个:
- 它生成重叠数组
- 它甚至比
for
循环更不直观
我的问题是:
有没有办法跳过循环,并以更“优雅”的方式进行(例如,通过使用一些 numpy
功能等)
提前致谢
尝试一下,不确定这是否有帮助,但在列表理解中使用 for 循环:
>>> import math
>>> new_size=10
使用列表理解创建列表列表:
>>> new_list = [[] for i in range(math.floor(a.size/new_size))]
使用列表理解复制到新列表
>>> [new_list[math.floor(i/new_size)].append(i) for i in a]
[None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
结果:
>>> new_list
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [10, 11, 12, 13, 14, 15, 16, 17, 18, 19], [20, 21, 22, 23, 24, 25, 26, 27, 28, 29], [30, 31, 32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47, 48, 49], [50, 51, 52, 53, 54, 55, 56, 57, 58, 59], [60, 61, 62, 63, 64, 65, 66, 67, 68, 69], [70, 71, 72, 73, 74, 75, 76, 77, 78, 79], [80, 81, 82, 83, 84, 85, 86, 87, 88, 89], [90, 91, 92, 93, 94, 95, 96, 97, 98, 99]]
>>>
如果您希望结果保留为 numpy 数组
np.array_split(a, 9)
[array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]),
array([12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]),
array([23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]),
array([34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44]),
array([45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55]),
array([56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66]),
array([67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77]),
array([78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88]),
array([89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])]
如果你想要列表作为输出
[i.tolist() for i in np.array_split(a, 9)]