测试两个列表是否相等[重复]

时间:2022-01-29 11:29:26

This question already has an answer here:

这个问题在这里已有答案:

I'm trying to write tests for my Django app and I need to check many times if 2 lists have the same objects (i.e. that every object in A is also in B and vice versa).

我正在尝试为我的Django app编写测试,我需要多次检查2个列表是否具有相同的对象(即A中的每个对象也在B中,反之亦然)。

I read on the assertLists/Sequence/Equal etc but for what I saw if the lists have the same objects but in a different order (A = [a,b,c], B = [b,c,a]) then it returns an error, which I don't want it to be an error because they both have the same objects.

我读了assertLists / Sequence / Equal等但是对于我所看到的,如果列表具有相同的对象但是顺序不同(A = [a,b,c],B = [b,c,a])那么它返回一个错误,我不希望它是一个错误,因为它们都有相同的对象。

Is there a way to check this without looping the lists?

有没有办法在不循环列表的情况下检查这个?

2 个解决方案

#1


9  

You can use assertCountEqual in Python 3, or assertItemsEqual in Python 2.

您可以在Python 3中使用assertCountEqual,或在Python 2中使用assertItemsEqual。

From the Python 3 docs for assertCountEqual:

来自assertCountEqual的Python 3文档:

Test that sequence first contains the same elements as second, regardless of their order. When they don’t, an error message listing the differences between the sequences will be generated.

测试该序列首先包含与第二个相同的元素,而不管它们的顺序如何。如果没有,将生成列出序列之间差异的错误消息。

Duplicate elements are not ignored when comparing first and second. It verifies whether each element has the same count in both sequences. Equivalent to: assertEqual(Counter(list(first)), Counter(list(second))) but works with sequences of unhashable objects as well.

比较第一个和第二个时,不会忽略重复的元素。它验证每个元素在两个序列中是否具有相同的计数。相当于:assertEqual(Counter(list(first)),Counter(list(second))),但也适用于不可用对象的序列。

#2


2  

If you are staying with list() datatype then the cleanest way if your lists are not too large would be :

如果您使用list()数据类型,那么如果列表不是太大,最简洁的方法是:

sorted(list_1) == sorted(list_2)

Have a look at this Question (which is the same): Check if two unordered lists are equal

看看这个问题(相同):检查两个无序列表是否相等

#1


9  

You can use assertCountEqual in Python 3, or assertItemsEqual in Python 2.

您可以在Python 3中使用assertCountEqual,或在Python 2中使用assertItemsEqual。

From the Python 3 docs for assertCountEqual:

来自assertCountEqual的Python 3文档:

Test that sequence first contains the same elements as second, regardless of their order. When they don’t, an error message listing the differences between the sequences will be generated.

测试该序列首先包含与第二个相同的元素,而不管它们的顺序如何。如果没有,将生成列出序列之间差异的错误消息。

Duplicate elements are not ignored when comparing first and second. It verifies whether each element has the same count in both sequences. Equivalent to: assertEqual(Counter(list(first)), Counter(list(second))) but works with sequences of unhashable objects as well.

比较第一个和第二个时,不会忽略重复的元素。它验证每个元素在两个序列中是否具有相同的计数。相当于:assertEqual(Counter(list(first)),Counter(list(second))),但也适用于不可用对象的序列。

#2


2  

If you are staying with list() datatype then the cleanest way if your lists are not too large would be :

如果您使用list()数据类型,那么如果列表不是太大,最简洁的方法是:

sorted(list_1) == sorted(list_2)

Have a look at this Question (which is the same): Check if two unordered lists are equal

看看这个问题(相同):检查两个无序列表是否相等