PuRaK - 10 months ago 54

Python Question

I want to compare two variables

`input_items`

`temp`

`print input_items`

prints -

`[array([ 50., 1., 0., ..., 0., 0., 0.], dtype=float32), array([ 50., -2., 0., ..., 0., 0., 0.], dtype=float32)]`

What's the best way to do that in Python?

Answer Source

I suppose that allclose good for your case because you need to compare floats

```
import numpy as np
a = np.arange(10)
print a
#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
b = np.arange(10)
print b
#array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print np.allclose(a, b)
#True
b[1] = 10
#array([ 0, 10, 2, 3, 4, 5, 6, 7, 8, 9])
print np.allclose(a, b)
#False
```

To compare lists of arrays you can combine np.allclose with all

```
a = [np.array([1, 2, 3]), np.array([1, 2, 3])]
b = [np.array([1, 2, 3]), np.array([1, 2, 3])]
all([np.allclose(x, y) for x, y in zip(a, b)])#True
b = [np.array([1, 2, 3]), np.array([1, 2, 4])]
all([np.allclose(x, y) for x, y in zip(a, b)])#False
```

PS Sorry for my poor English