obchardon
obchardon

Reputation: 10792

How to check if a number is already in the first column of a 2d numpy array

I try to vstack an array A1 with an array A2 if the first number of the array A2 is not in the first column of the array A1:

A1 = np.array([])
numbers = [1,2,3,3,4,4,5,6,7,8,9]
for number in numbers:
   if number not in A1[:,0]:
      A2 = [number,10]
      A1 = np.vstack((A1,A2))

But of course at the beggining A1 is not a 2D array, so A1[:,0] will produce an IndexError: too many indices for array.

At the moment I create a dummy empty 2D array and then I supress the dummy values:

A1 = np.zeros(shape=(2,2))
numbers = [1,2,3,3,4,4,5,6,7,8,9]

for number in numbers:
   if number not in A1[:,0]:
      A2 = [number, 10]
      A1 = np.vstack((A1,A2))
A1 = A1[2:,:]

But, ok... we all agree that it's disgusting to do that...

How can I resolve this problem ?

Upvotes: 2

Views: 653

Answers (1)

jpp
jpp

Reputation: 164773

It is inefficient and generally not a good idea to operate on numpy arrays as if they were lists.

Instead, use the vectorised functionality available to numpy. Below is an example.

import numpy as np

numbers = [1,2,3,3,4,4,5,6,7,8,9]

unique = np.unique(numbers)
constant = np.ones(len(unique)) * 10

A1 = np.vstack((unique, constant)).T

If order is important, use this instead:

idx = np.unique(numbers, return_index=True)[1]
unique = numbers[sorted(idx)]

Upvotes: 1

Related Questions