在Python中比较两个包含数组的列表

时间:2015-02-25 14:42:11

标签: python arrays list

我有两个Python列表,每个列表都包含一个数组。 e.g

list1[0]
out: array([[ 1, 2, 3],[ 4, 5, 6], . . .]], dtype=float32)

list2[0]
out: array([[ 1, 5, 3],[ 4, 5, 6], . . .]], dtype=float32)    

and list1[0][0]
out: array([ 1, 2, 3], dtype=float32)

and list2[0][0]
out: array([ 1, 5, 3], dtype=float32)

列表包含表示3d空间中的线条的点。 我想检查这两个列表是否相同,即它们是否代表3d空间中的相同行。我尝试了all(),any(),set()等,但这些函数适用于包含非数组的列表。有任何想法吗?

2 个答案:

答案 0 :(得分:0)

在比较两个列表的长度后,您可以迭代并使用NumPy的array_equal函数:

using numpy as np

def listsEqual(list1, list2):
    return len(list1) == len(list2) and np.all([np.array_equal(l1, l2) for l1, l2 in zip(list1, list2)])

答案 1 :(得分:0)

首先检查阵列是否具有相同的形状,然后重新整形并检查它们是否相同 -

list1.shape == list2.shape and np.array_equal(list1.reshape(-1,), list2.reshape(-1,))

list1.shape == list2.shape and np.all(list1.reshape(-1,) == list2.reshape(-1,))