Issue
I have an array like so:
['6.00', '1.85', 'X', '2.25', '1.26', 'X', '6.50', '1.80', 'X', '6.50', '1.95', 'X', '34.00', '5.50']
Does anyone know how I could return an array of every 3rd element before 'X'?:
['6.00', '2.25', '6.50', '6.50']
I have been removing every 2nd and 3rd element by the following code, but it breaks when there are more elements past the last 'X' in the list or when the 'X' is not evenly spaced:
data = ['6.00', '1.85', 'X', '2.25', '1.26', 'X', '6.50', '1.80', 'X', '6.50', '1.95', 'X', '34.00', '5.50']
x = np.array(data)
x = np.delete(x, np.arange(1, x.size, 3))
x = np.delete(x, np.arange(1, x.size, 2))
Solution
By using offset of positions (found by np.nonzero
) where x == 'X'
:
x[np.nonzero(x == 'X')[0] - 2]
array(['6.00', '2.25', '6.50', '6.50'], dtype='<U5')
Answered By - RomanPerekhrest
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.