51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

如何在 Python 中比较两个列表

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

解释:

在上面的例子中,我们已经声明了要相互比较的两个列表。我们将这些列表转换成集合,并在==运算符的帮助下比较每个元素。两个列表中的所有元素都是相等的,那么如果执行了 block 并打印了结果。

带有==运算符的 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模块提供计数器(),,有效比较列表。它以字典格式<值> : <频率>存储数据,并计算列表项目的频率。

注意------列表元素的顺序在这个函数中并不重要。

示例-

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 可迭代对象(列表、元组、字符串等)作为参数,并返回一个 map 对象。该函数对列表的每个元素实现,并返回一个迭代器作为结果。

此外, 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 中比较两个列表的各种方法。


赞(6)
未经允许不得转载:工具盒子 » 如何在 Python 中比较两个列表