python判断两个列表相等_python – 测试两个列表是否相等

参见英文答案 > Check if two unordered lists are equal                                    8个

我正在尝试为我的Django应用程序编写测试,我需要多次检查

如果2个列表具有相同的对象(即A中的每个对象也在B中,反之亦然).

我读了assertLists / Sequence / Equal等,但是我看到的是列表

有相同的对象,但顺序不同(A = [a,b,c],B = [b,c,a])然后它返回一个错误,我不希望它是一个错误,因为他们俩有相同的对象.

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

解决方法:

来自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.

标签:python,unit-testing,django

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值