0

Looking for a way to store this code in a 2D array in python. I've tried making a 1D array and then turning it into a 2D array but my code is still cumbersome and not working. The gap between 4 and 6 is not a typo. Any help would be greatly appreciated.

recno1inds11 = nonzero(data11[:,1]==no1)[0]
recno2inds11 = nonzero(data11[:,1]==no2)[0]
recno3inds11 = nonzero(data11[:,1]==no3)[0]
recno4inds11 = nonzero(data11[:,1]==no4)[0]

recno6inds11 = nonzero(data11[:,1]==no6)[0]
recno7inds11 = nonzero(data11[:,1]==no7)[0]
recno8inds11 = nonzero(data11[:,1]==no8)[0]
recno9inds11 = nonzero(data11[:,1]==no9)[0]
recno10inds11 = nonzero(data11[:,1]==no10)[0]
recno11inds11 = nonzero(data11[:,1]==no11)[0]
recno12inds11 = nonzero(data11[:,1]==no12)[0]
recno13inds11 = nonzero(data11[:,1]==no13)[0]
recno14inds11 = nonzero(data11[:,1]==no14)[0]
recno15inds11 = nonzero(data11[:,1]==no15)[0] 
recno16inds11 = nonzero(data11[:,1]==no16)[0]
recno17inds11 = nonzero(data11[:,1]==no17)[0]
recno18inds11 = nonzero(data11[:,1]==no18)[0]
recno19inds11 = nonzero(data11[:,1]==no19)[0]
recno20inds11 = nonzero(data11[:,1]==no20)[0]
recno21inds11 = nonzero(data11[:,1]==no21)[0] 
recno22inds11 = nonzero(data11[:,1]==no22)[0]
recno23inds11 = nonzero(data11[:,1]==no23)[0]
recno24inds11 = nonzero(data11[:,1]==no24)[0]
recno25inds11 = nonzero(data11[:,1]==no25)[0]
recno26inds11 = nonzero(data11[:,1]==no26)[0]
recno27inds11 = nonzero(data11[:,1]==no27)[0]
recno28inds11 = nonzero(data11[:,1]==no28)[0]
recno29inds11 = nonzero(data11[:,1]==no29)[0]
recno30inds11 = nonzero(data11[:,1]==no30)[0]

1 Answer 1

3

Normally, you don't want to have 30 separate variables like this, you want to have an array of 30 values.

And if you had that, this would be a one-liner; you could need to transpose the right-hand array into the second axis, then use the == operator.

>>> data11 = np.array([[1,2,3],[4,5,6],[7,8,9]])
>>> data11[:,1]
array([2, 5, 8])
>>> no1to5 = np.array([1, 2, 3, 4, 5])
>>> data11[:,1] == no1to5.reshape((5,1))
array([[False, False, False],
       [ True, False, False],
       [False, False, False],
       [False, False, False],
       [False,  True, False]], dtype=bool)

Of course you can also apply nonzero, grab the first axis, … whatever you want to do, you can vectorize it as long as you have a vector in the first place, instead of a big collection of separate values that are only related by the meta-information in the variable names you happen to have bound them to.

Sign up to request clarification or add additional context in comments.

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.