numpy 在www.example.com for Python中,如何将数组拆分为特定的间隔Num.py?

e5nszbig  于 2022-11-23  发布在  Python
关注(0)|答案(1)|浏览(113)

问题是这样的:
x = NP范围(100)
编写Python代码,以这些间隔拆分以下数组:第10、25、45、75、95页
我已经使用了分割功能,但无法获得这些特定的间隔,有人能告诉我另一种方法吗?或者我做错了?

wj8zmpe1

wj8zmpe11#

这里有手动方式和麻木方式与分裂。

# Manual method
x = np.arange(100)
split_indices = [10, 25, 45, 75, 95]

split_arrays = []
for i, j in zip([0]+split_indices[:-1], split_indices):
    split_arrays.append(x[i:j])

print(split_arrays)

# Numpy method
split_arrays_np = np.split(x, split_indices)
print(split_arrays_np)

而结果是(对两者而言)

[array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), 
 array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]), 
 array([25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44]), 
 array([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]),
 array([75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94])
]

相关问题