One approach with boolean-indexing would be -
mask = np.in1d(np.arange(A.size),B)
out = np.r_[A[mask], A[~mask]]
Sample run -
In [26]: A = np.array([ 1, 2, 5, 9.8, 55, 3])
In [27]: B = np.array([ 3, 4])
In [28]: mask = np.in1d(np.arange(A.size),B)
In [29]: np.r_[A[mask], A[~mask]]
Out[29]: array([ 9.8, 55. , 1. , 2. , 5. , 3. ])
Another with integer-indexing -
idx = np.setdiff1d(np.arange(A.size),B)
out = np.r_[A[B], A[idx]]
Sample run -
In [36]: idx = np.setdiff1d(np.arange(A.size),B)
In [37]: np.r_[A[B], A[idx]]
Out[37]: array([ 9.8, 55. , 1. , 2. , 5. , 3. ])
Answer from Divakar on Stack OverflowOne approach with boolean-indexing would be -
mask = np.in1d(np.arange(A.size),B)
out = np.r_[A[mask], A[~mask]]
Sample run -
In [26]: A = np.array([ 1, 2, 5, 9.8, 55, 3])
In [27]: B = np.array([ 3, 4])
In [28]: mask = np.in1d(np.arange(A.size),B)
In [29]: np.r_[A[mask], A[~mask]]
Out[29]: array([ 9.8, 55. , 1. , 2. , 5. , 3. ])
Another with integer-indexing -
idx = np.setdiff1d(np.arange(A.size),B)
out = np.r_[A[B], A[idx]]
Sample run -
In [36]: idx = np.setdiff1d(np.arange(A.size),B)
In [37]: np.r_[A[B], A[idx]]
Out[37]: array([ 9.8, 55. , 1. , 2. , 5. , 3. ])
Non-numpy answer.
A = [ 1, 2, 5, 9.8, 55, 3]
B = [ 3, 4]
new_arr = [A[i] for i in B if i<len(A)] + [A[i] for i in range(len(A)) if i not in set(B)]
# [9.8, 55, 1, 2, 5, 3]
You may remove the extra check if i<len(A) if you are sure about it.
You can play around with the name of field.
r=[(x['SHAPE@XY'][i][0],x['SHAPE@XY'][i][1],x['PrimeKey'][i]) for i in range(x.shape[0])]
x=np.rec.fromrecords(r, formats = 'f8,f8,i4', names = 'SHAPE@X,SHAPE@Y,PrimeKey')
rec.array([(506173.7478, 5455684.263900001, 1),
(506175.22869999986, 5455648.723099999, 2),
(506229.03359999973, 5455661.5572999995, 3),
(506250.25939999986, 5455614.169500001, 4),
(506305.54509999976, 5455579.122300001, 5),
(506331.70710000023, 5455688.2129, 6)],
dtype=[('SHAPE@X', '<f8'), ('SHAPE@Y', '<f8'), ('PrimeKey', '<i4')])
Something like this:
>>> numpy.array([(x[0][0], x[0][1], x[1]) for x in locxyarray], dtype=np.dtype([('X', np.float), ('Y', np.float), ('PrimeKey', np.int32)]))
array([(506173.7478, 5455684.263900001, 1),
(506175.22869999986, 5455648.723099999, 2),
(506229.03359999973, 5455661.5572999995, 3),
(506250.25939999986, 5455614.169500001, 4),
(506305.54509999976, 5455579.122300001, 5),
(506331.70710000023, 5455688.2129, 6)],
dtype=[('X', '<f8'), ('Y', '<f8'), ('PrimeKey', '<i4')])
Note that dtype can't be kept the same because now the array elements consist of three fields instead of two in the original.
Use numpy.delete(), which returns a new array with sub-arrays along an axis deleted.
numpy.delete(a, index)
For your specific question:
import numpy as np
a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
index = [2, 3, 6]
new_a = np.delete(a, index)
print(new_a)
# Output: [1, 2, 5, 6, 8, 9]
Note that numpy.delete() returns a new array since array scalars are immutable, similar to strings in Python, so each time a change is made to it, a new object is created. I.e., to quote the delete() docs:
"A copy of arr with the elements specified by obj removed. Note that delete does not occur in-place..."
If the code I post has output, it is the result of running the code.
Use np.setdiff1d:
import numpy as np
>>> a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
>>> b = np.array([3,4,7])
>>> c = np.setdiff1d(a,b)
>>> c
array([1, 2, 5, 6, 8, 9])
You needn't do that in two lines. Numpy allows you to splice within a single statement, like this:
room_matrix[1:3, 1:3]
#will slice rows starting from 1 to 2 (row numbers start at 0), likewise for columns
How about this:
In [1]: import numpy as np
In [2]: room_matrix = \
...: np.array(
...: [[6, 3, 4, 1],
...: [5, 2, 3, 2],
...: [8, 3, 6, 2],
...: [5, 1, 3, 1],
...: [10, 4, 7, 2]])
In [3]: room_matrix
Out[3]:
array([[ 6, 3, 4, 1],
[ 5, 2, 3, 2],
[ 8, 3, 6, 2],
[ 5, 1, 3, 1],
[10, 4, 7, 2]])
In [4]: room_matrix[1:3, 1:3]
Out[4]:
array([[2, 3],
[3, 6]])