Python检查两个列表是否相同

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

本文涉及检查两个无序列表是否在位置完全相似的位置包含完全相似的元素的方法, 即检查两个列表是否完全相等。这是一个非常有用的实用程序, 可以在日常编程中使用。

方法1:使用

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

方法2:使用

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

方法3:使用

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

方法4:使用

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

注意怪胎!巩固你的基础Python编程基础课程和学习基础知识。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: