這是我的代碼。如何比較兩個集合,其中的每個元素是列表?
a = [
['StarList', 'StarId38', 'ShipList']
]
b = [
['StarList', 'StarId3', 'ShipList'],
['StarList', 'StarId4', 'ShipList']
]
assert set(a) == set(b) # False
a = [
['StarList', 'StarId4', 'ShipList'],
['StarList', 'StarId3', 'ShipList']
]
assert set(a) == set(b) # True
它不工作:
Traceback (most recent call last):
File "compare.py", line 8, in <module>
assert set(a) == set(b) # False
TypeError: unhashable type: 'list'
好了,該怎麼辦呢?
這會創建中間列表,集合和元組,這可能會非常好。如果更改子列表的順序是可以接受的,請對每個子列表進行排序然後進行比較。 – 2013-04-25 19:47:13