# 检查两个无序列表是否相等

``['one', 'two', 'three'] == ['one', 'two', 'three'] : true ['one', 'two', 'three'] == ['one', 'three', 'two'] : true ['one', 'two', 'three'] == ['one', 'two', 'three', 'three'] : false ['one', 'two', 'three'] == ['one', 'two', 'three', 'four'] : false ['one', 'two', 'three'] == ['one', 'two', 'four'] : false ['one', 'two', 'three'] == ['one'] : false` `

Python有一个内置的数据types，用于无序集合（可哈希），称为`set` 。 如果将两个列表转换为集合，比较将是无序的。

` `set(x) == set(y)` `

`set`文件

` `>>> import collections >>> compare = lambda x, y: collections.Counter(x) == collections.Counter(y) >>> >>> compare([1,2,3], [1,2,3,3]) False >>> compare([1,2,3], [1,2,3]) True >>> compare([1,2,3,3], [1,2,2,3]) False >>>` `

` `>>> a = [1,1,2] >>> b = [1,2,2] >>> a.sort() >>> b.sort() >>> a == b False` `

` `>>> set(['one', 'two', 'three']) == set(['two', 'one', 'three']) True` `

` `>>> set(['one', 'one', 'one']) == set(['one']) True` `

` `def are_eq(a, b): return set(a) == set(b) and len(a) == len(b)` `
` `sorted(x) == sorted(y)` `

1. 正如在这个答案中所指出的，它比使用计数器更好
2. x.sort（）对x进行sorting，这是一个副作用。 sorting（x）返回一个新的列表。

` `sum([1 for i,j in zip(a,b) if i==j])` `

` `len(a)==len(b) and len(a)==sum([1 for i,j in zip(a,b) if i==j])` `

` `compare = lambda a,b: len(a)==len(b) and len(a)==sum([1 for i,j in zip(a,b) if i==j])` `

` `>>> compare([1,2,3], [1,2,3,3]) False >>> compare([1,2,3], [1,2,3]) True >>> compare([1,2,3], [1,2,4]) False` `

` `if ((len(list1) == len(list2)) and (all(i in list2 for i in list1))): print 'True' else: print 'False'` `

` `if all(i in list2 for i in list1): print 'True' else: print 'False'` `

` `functools.reduce(lambda b1,b2: b1 and b2, map(lambda e1,e2: e1==e2, listA, ListB), True)` `

>>>从functools导入减less
>>> def compvecs（a，b）：
… return reduce（lambda b1，b2：b1和b2，map（lambda e1，e2：e1 == e2，a，b），True）

>>> compvecs（a = [1,2,3,4]，b = [1,2,4,3]）

>>> compvecs（a = [1,2,3,4]，b = [1,2,3,4]）

>>> compvecs（a = [1,2,3,4]，b = [1,2,4,3]）

>>> compare_vectors（a = [1,2,3,4]，b = [1,2,2,4]）

>>>

` `>>> l1 = ['one', 'two', 'three'] >>> l2 = ['one', 'two', 'three'] >>> l3 = ['one', 'three', 'two'] >>> print str(l1) == str(l2) True >>> print str(l1) == str(l3) False` `