Skip to content

Instantly share code, notes, and snippets.

@kattoyoshi
Last active June 24, 2018 13:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kattoyoshi/6c691033a4951a4607e85fed93841dcf to your computer and use it in GitHub Desktop.
Save kattoyoshi/6c691033a4951a4607e85fed93841dcf to your computer and use it in GitHub Desktop.
Examples of the comparison of the array data ( list and numpy ndarray).
import numpy as np
####################################
# Comparison of the 'list'
####################################
# In case of the 'list' data,
# the result of the comparison using '==' and/or '!='
# is whether all the elements meet the condition.
# The result is just 'True' or 'False', not elementwise.
test_data = [[1,2,3],[4,5,6]]
test_data1 = [[0,2,3],[4,5,6]]
actual = [[1,2,3],[4,5,6]]
print(test_data == actual)
# [out] >> True
print(test_data1 == actual)
# [out] >> False
##############################################
# Comparison of the numpy 'ndarray'
##############################################
# In case of the 'ndarray' data,
# the result of the comparison using '==' and/or '!=' is elementwise.
# If you want to check whether all the elements meet the condition,
# use '.all()' command of the numpy or 'all()' command of the native python.
test_data = np.array([1,2,3])
test_data1 = np.array([0,2,3])
actual = np.array([1,2,3])
# element wise comparison
print(test_data == actual)
# [out] >> [True, True, True]
print(test_data1 == actual)
# [out] >> [False, True, True]
# comparison of all the elements
# Using .all() command of the numpy
print((test_data == actual).all())
# [out] >> True
print((test_data1 == actual).all())
# [out] >> False
# Using all() command of the native python
print(all(test_data == actual))
# [out] >> True
print(all(test_data1 == actual))
# [out] >> False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment