PuRaK PuRaK - 13 days ago 5
Python Question

How to compare list of arrays in python?

I want to compare two variables

input_items
and
temp
for equality. To give you an idea of their datatype -

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

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