1

The below code checks if each element of sub-arrays is greater than 2 and gives the result in their respective sub-arrays:

2d_array=np.array([[1,2,3,4],[4,56,7,1]])
for elem in 2d_array:
    print(elem[elem[:]>2])

Output:

[3 4]
[ 4 56  7]

Can we do the same without using a for loop, Preferably using numpy functions.

1 Answer 1

1

Numpy produces arrays. In your case, each resulting line has a different length, so you can't get an array, just a list of lists.

However, if you want to gather all the values into a 1D array, you can just do

vaulues = array_2d[array_2d > 2]
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.