2

I have a list where every value is presented only one, and I have another list with the desired order of a tokenized numpy array.

For instance:

sorted_values = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
a = ['a', 'c', 'g']
b = ['e']

I want to convert a and b (in a efficient) to a numpy array like this:

at = [1,0,1,0,0,0,1]
bt = [0,0,0,0,1,0,0]

Is there any efficient way to do this?

0

2 Answers 2

3

Using the fact that the first array is already sorted, we could employ np.searchsorted for efficiency -

at = np.zeros(len(sorted_values), dtype=int)
bt = at.copy()
at[np.searchsorted(sorted_values, a)] = 1
bt[np.searchsorted(sorted_values, b)] = 1
Sign up to request clarification or add additional context in comments.

Comments

2

You can use np.in1d:

np.in1d(sorted_values, a).astype(int)
#array([1, 0, 1, 0, 0, 0, 1])

np.in1d(sorted_values, b).astype(int)
#array([0, 0, 0, 0, 1, 0, 0])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.