Python提供了多种比较两个列表的方法。比较是将一个列表的数据项与另一个列表的数据项进行检查,看它们是否相同的过程。

list1 - [11, 12, 13, 14, 15]  
list2 - [11, 12, 13, 14, 15]  
Output - The lists are equal  

比较两个列表的方法如下:

  • cmp() 函数
  • set() 函数和 == 运算符
  • sort() 函数和 == 运算符
  • collection.counter() 函数
  • reduce() 和 map() 函数

cmp() 函数

Python cmp() 函数比较两个Python对象,并根据比较返回整数值 -1、0、1。

注意 - 它不在Python 3.x版本中使用。

set() 函数和 == 运算符

Python的set()函数将列表转化为集合,不考虑元素的顺序。此外,我们使用等于运算符(==)来比较列表的数据项。让我们通过以下示例来理解。

示例 -

list1 = [11, 12, 13, 14, 15]  
list2 = [12, 13, 11, 15, 14]  
  
a = set(list1)  
b = set(list2)  
  
if a == b:  
    print("The list1 and list2 are equal")  
else:  
    print("The list1 and list2 are not equal")  

输出:

The list1 and list2 are equal

解释:

在上面的示例中,我们声明了两个要进行比较的列表。我们将这些列表转换为集合,并使用==运算符比较每个元素。由于两个列表中的所有元素都相等,因此执行if块并打印结果。

使用 sort() 方法和 == 运算符

Python的sort()函数用于对列表进行排序。同一个列表的元素在相同的索引位置,这意味着这两个列表是相等的。

注意 - 在sort()方法中,我们可以以任何顺序传递列表项,因为我们在比较之前对列表进行了排序。

让我们通过以下示例来理解。

示例 -

import collections  
  
list1 = [10, 20, 30, 40, 50, 60]  
list2 = [10, 20, 30, 50, 40, 70]  
list3 = [50, 10, 30, 20, 60, 40]  
  
# Sorting the list  
list1.sort()  
list2.sort()  
list3.sort()  
  
  
if list1 == list2:  
    print("The list1 and list2 are the same")  
else:  
    print("The list1 and list3 are not the same")  
  
if list1 == list3:  
    print("The list1 and list2 are not the same")  
else:  
    print("The list1 and list2 are not the same")  

输出:

The list1 and list3 are not the same
The list1 and list2 are not the same

collection.counter() 函数

collections模块提供了counter()函数,它可以高效地比较列表。它以字典格式存储数据:<值>:<频率>,并计算列表项的频率。

注意 - 在此函数中,列表元素的顺序不重要。

示例 -

import collections  
  
list1 = [10, 20, 30, 40, 50, 60]  
list2 = [10, 20, 30, 50, 40, 70]  
list3 = [50, 10, 30, 20, 60, 40]  
  
  
if collections.Counter(list1) == collections.Counter(list2):  
    print("The lists l1 and l2 are the same")  
else:  
    print("The lists l1 and l2 are not the same")  
  
if collections.Counter(list1) == collections.Counter(list3):  
    print("The lists l1 and l3 are the same")  
else:  
    print("The lists l1 and l3 are not the same")  

输出:

The lists list1 and list2 are not the same
The lists list1 and list3 are the same

reduce() 和 map()

map() 函数接受一个函数和Python可迭代对象(列表、元组、字符串等)作为参数,并返回一个映射对象。该函数对列表的每个元素实施,返回一个迭代器作为结果。

此外,reduce() 方法会递归地实施给定的函数到可迭代对象上。

在这里,我们将同时使用这两种方法。map() 函数将函数(可以是用户定义的函数或 lambda 函数)应用于每个可迭代对象,而 reduce() 函数则负责以递归方式应用它。

注意 - 我们需要导入 functool 模块才能使用 reduce() 函数。

让我们通过以下示例来理解。

示例 -

import functools  
  
list1 = [10, 20, 30, 40, 50]  
list2 = [10, 20, 30, 50, 40, 60, 70]  
list3 = [10, 20, 30, 40, 50]  
  
if functools.reduce(lambda x, y: x and y, map(lambda a, b: a == b, list1, list2), True):  
    print("The list1 and list2 are the same")  
else:  
    print("The list1 and list2 are not the same")  
  
if functools.reduce(lambda x, y: x and y, map(lambda a, b: a == b, list1, list3), True):  
    print("The list1 and list3 are the same")  
else:  
    print("The list1 and list3 are not the same")  

输出:

The list1 and list2 are not the same
The list1 and list3 are the same

在本节中,我们介绍了在Python中比较两个列表的各种方法。

标签: Tkinter教程, Tkinter安装, Tkinter库, Tkinter入门, Tkinter学习, Tkinter入门教程, Tkinter, Tkinter进阶, Tkinter指南, Tkinter学习指南, Tkinter进阶教程, Tkinter编程