如何比较两个不同顺序的列表?

时间:2021-01-04 11:25:34

I need to compare two lists without taking their order into account:

我需要比较两个列表而不考虑它们的顺序:

list_a = ['one', 'two', 'three']
list_b = ['three', 'one', 'two']

when I try to compare them, it returns False:

当我尝试比较它们时,它返回False:

>>> list_a == list_b
False

Both lists have many elements, what would be the optimal way to compare them?

两个列表都有很多元素,比较它们的最佳方法是什么?

3 个解决方案

#1


8  

You need to compare whether both lists result in the same Counter.

您需要比较两个列表是否产生相同的计数器。

>>> from collections import Counter
>>> list_a = ['one', 'two', 'three']
>>> list_b = ['three', 'one', 'two']
>>> Counter(list_a) == Counter(list_b)
True
>>> list_b = ['three', 'one', 'two', 'two']
>>> Counter(list_a) == Counter(list_b)
False
>>> set(list_a) == set(list_b)
True # False positive

Another solution would be to sort both lists and then compare them. The Counter approach should be more efficient for big lists since it has linear time complexity (i.e. O(n)) while sorting is only pseudo-linear (i.e. O(n*log(n)).

另一种解决方案是对两个列表进行排序,然后进行比较。 Counter方法对于大列表应该更有效,因为它具有线性时间复杂度(即O(n)),而排序仅是伪线性的(即O(n * log(n))。

#2


6  

One way would be to compare a set of each list

一种方法是比较每个列表的集合

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

Otherwise if there may be duplicates, and you want to ensure they have the same number of each element then you could do

否则,如果可能存在重复项,并且您希望确保它们具有相同数量的每个元素,那么您可以这样做

>>> sorted(list_a) == sorted(list_b)
True

#3


1  

Counter of module collections will be the way to go. As you have strings and as show in the answers:

模块集合的计数器将是最佳选择。因为你有字符串,并在答案中显示:

from collections import Counter
.....
if Counter(list_a) == Counter(list_b): # True or False

if the list have any unhashable elements (other than string and number), like objects etc, you might want to take out their id and make another list and compare their Counter.

如果列表有任何不可用的元素(字符串和数字除外),如对象等,你可能想要取出他们的id并制作另一个列表并比较他们的计数器。

...
if Counter(map(id,list_a)) == Counter(map(id,list_b)):
    print "same unhashable things in list_a and list_b"

#1


8  

You need to compare whether both lists result in the same Counter.

您需要比较两个列表是否产生相同的计数器。

>>> from collections import Counter
>>> list_a = ['one', 'two', 'three']
>>> list_b = ['three', 'one', 'two']
>>> Counter(list_a) == Counter(list_b)
True
>>> list_b = ['three', 'one', 'two', 'two']
>>> Counter(list_a) == Counter(list_b)
False
>>> set(list_a) == set(list_b)
True # False positive

Another solution would be to sort both lists and then compare them. The Counter approach should be more efficient for big lists since it has linear time complexity (i.e. O(n)) while sorting is only pseudo-linear (i.e. O(n*log(n)).

另一种解决方案是对两个列表进行排序,然后进行比较。 Counter方法对于大列表应该更有效,因为它具有线性时间复杂度(即O(n)),而排序仅是伪线性的(即O(n * log(n))。

#2


6  

One way would be to compare a set of each list

一种方法是比较每个列表的集合

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

Otherwise if there may be duplicates, and you want to ensure they have the same number of each element then you could do

否则,如果可能存在重复项,并且您希望确保它们具有相同数量的每个元素,那么您可以这样做

>>> sorted(list_a) == sorted(list_b)
True

#3


1  

Counter of module collections will be the way to go. As you have strings and as show in the answers:

模块集合的计数器将是最佳选择。因为你有字符串,并在答案中显示:

from collections import Counter
.....
if Counter(list_a) == Counter(list_b): # True or False

if the list have any unhashable elements (other than string and number), like objects etc, you might want to take out their id and make another list and compare their Counter.

如果列表有任何不可用的元素(字符串和数字除外),如对象等,你可能想要取出他们的id并制作另一个列表并比较他们的计数器。

...
if Counter(map(id,list_a)) == Counter(map(id,list_b)):
    print "same unhashable things in list_a and list_b"