2017-11-09 3 views

答えて

1

私はシンプルアサート平等のテストは大丈夫でなければなりません推測:unittest

>>> d1 = {n: chr(n+65) for n in range(10)} 
>>> d2 = {n: chr(n+65) for n in range(10)} 
>>> d1 == d2 
True 
>>> l1 = [1, 2, 3] 
>>> l2 = [1, 2, 3] 
>>> d2[10] = l2 
>>> d1[10] = l1 
>>> d2 
>>> d1 == d2 
True 
>>> class Example: 
    stub_prop = None 
>>> e1 = Example() 
>>> e2 = Example() 
>>> e2.stub_prop = 10 
>>> e1.stub_prop = 'a' 
>>> d1[11] = e1 
>>> d2[11] = e2 
>>> d1 == d2 
False 
0

、あなたは辞書を値として複雑なオブジェクトまたはコレクションを持つためにself.assertDictEqual(d1, d2)を使用することができます。 assertDictEqualは、2つの辞書の間の正確な違いを、それらが同一でない場合に提供します。

assert d1 == d2は、pytestと同じ方法で動作しますが、違いが何であるかはわかりません。

1

あなたの質問は非常に具体的ではありませんが、私が理解できるもので、どちらかのリストの値が同じ

import collections 

compare = lambda x, y: collections.Counter(x) == collections.Counter(y) 
compare([1,2,3], [1,2,3,3]) 
print compare #answer would be false 
compare([1,2,3], [1,2,3]) 
print compare #answer would be true 
ある場合は、長さが

a = [1,5,3,6,3,2,4] 
b = [5,3,2,1,3,5,3] 

if (len(a) == len(b)): 
    print True 
else: 
    print false 

かチェックし、同じかどうかを確認しようとしています

しかし、辞書のために、あなたはまた、あなたがD1 == d2`を主張 `みました

x = dict(a=1, b=2) 
y = dict(a=2, b=2) 

if(x == y): 
    print True 
else: 
    print False 
関連する問題