0

I want to compare all elements of a two-dimensional list array with a float x. The result should be a list: b = [[True, False,...],...]. I tried it like that:

import numpy as np

array = [[a1,a2,...], [a3,a4,...],...,] 
x = 2.0

b = np.array([a >= x for a in array])`

"TypeError: '>=' not supported between instances of 'list' and 'float'"

When I use a one-dimensional list it works fine.

Thanks in advance!!

5
  • array = np.array([[a1,a2,...], [a3,a4,...],...,] ) and if you still get the error, that means your list elements are ragged (irregular in size). Commented Apr 24, 2018 at 18:43
  • 1
    array is a list of lists, but x. is a scalar Commented Apr 24, 2018 at 18:43
  • 2
    In you code b = np.array([a >= x for a in array]), since array is multidimensional, a is a list. So in this case you are comparing a (a list) with x (a float). Commented Apr 24, 2018 at 18:47
  • b = np.array(array) >= x? Commented Apr 24, 2018 at 18:48
  • Can you provide an example of a portion of array, say array[:2,:2]? Commented Apr 24, 2018 at 18:54

1 Answer 1

1
b = np.array([[a >= x for a in row] for row in array])
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.