# Python检查两个列表是否相同

2021年3月13日15:37:59 发表评论 414 次浏览

list.sort()

==

==

``````# Python 3 code to demonstrate
# check if list are identical
# using sort() + == operator

# initializing lists
test_list1 = [ 1 , 2 , 4 , 3 , 5 ]
test_list2 = [ 1 , 2 , 4 , 3 , 5 ]

# printing lists
print ( "The first list is : " + str (test_list1))
print ( "The second list is : " + str (test_list2))

# sorting both the lists
test_list1.sort()
test_list2.sort()

# using == to check if
# lists are equal
if test_list1 = = test_list2:
print ( "The lists are identical" )
else :
print ( "The lists are not identical" )``````

``````The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical``````

collections.Counter()

, 我们通常能够获取列表中每个元素的频率, 进行检查, 对于两个列表, 我们都可以检查两个列表是否相同。但是, 此方法也忽略列表中元素的顺序, 仅考虑元素的频率。

``````# Python 3 code to demonstrate
# check if list are identical
# using collections.Counter()
import collections

# initializing lists
test_list1 = [ 1 , 2 , 4 , 3 , 5 ]
test_list2 = [ 1 , 2 , 4 , 3 , 5 ]

# printing lists
print ( "The first list is : " + str (test_list1))
print ( "The second list is : " + str (test_list2))

# using collections.Counter() to check if
# lists are equal
if collections.Counter(test_list1) = = collections.Counter(test_list2):
print ( "The lists are identical" )
else :
print ( "The lists are not identical" )``````

``````The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical``````

sum()+ zip()+ len()

sum()+ zip()

, 如果两个列表中的两个索引都具有相等的元素, 则可以将列表之一的总和作为1的总和, 然后将该数字与其他列表的大小进行比较。这也需要首先在计算之前检查两个列表是否相等。它还会检查订单。

``````# Python 3 code to demonstrate
# check if list are identical
# using sum() + zip() + len()

# initializing lists
test_list1 = [ 1 , 2 , 4 , 3 , 5 ]
test_list2 = [ 1 , 2 , 4 , 3 , 5 ]

# printing lists
print ( "The first list is : " + str (test_list1))
print ( "The second list is : " + str (test_list2))

# using sum() + zip() + len() to check if
# lists are equal
if len (test_list1) = = len (test_list2) and len (test_list1) = = sum ([ 1 for i, j in zip (test_list1, test_list2) if i = = j]):
print ( "The lists are identical" )
else :
print ( "The lists are not identical" )``````

``````The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical``````

reduce()+ map()

, 我们可以完成检查两个列表是否相同的任务。这也考虑了列表的顺序。

``````# Python 3 code to demonstrate
# check if list are identical
# using map() + reduce()
import functools

# initializing lists
test_list1 = [ 1 , 2 , 4 , 3 , 5 ]
test_list2 = [ 1 , 2 , 4 , 3 , 5 ]

# printing lists
print ( "The first list is : " + str (test_list1))
print ( "The second list is : " + str (test_list2))

# using map() + reduce() to check if
# lists are equal
if functools. reduce ( lambda i, j : i and j, map ( lambda m, k: m = = k, test_list1, test_list2), True ) :
print ( "The lists are identical" )
else :
print ( "The lists are not identical" )``````

``````The first list is : [1, 2, 4, 3, 5]
The second list is : [1, 2, 4, 3, 5]
The lists are identical``````