12
我正在尋找一個numpy的功能,會做的相當於列表的值:numpy的功能,數組中的元素設置爲給定的指標
indices = set([1, 4, 5, 6, 7])
zero = numpy.zeros(10)
for i in indices:
zero[i] = 42
我正在尋找一個numpy的功能,會做的相當於列表的值:numpy的功能,數組中的元素設置爲給定的指標
indices = set([1, 4, 5, 6, 7])
zero = numpy.zeros(10)
for i in indices:
zero[i] = 42
你只需給它的索引列表:
indices = [1, 4, 5, 6, 7]
zero = numpy.zeros(10)
zero[indices] = 42
如果你有一個ndarry:
>>> x = np.zeros((3, 3, 3))
>>> y = [0, 9, 18]
>>> x
array([[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 0., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]]])
>>> np.put(x, y, 1)
>>> x
array([[[ 1., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 1., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]],
[[ 1., 0., 0.],
[ 0., 0., 0.],
[ 0., 0., 0.]]])