如何在 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 中比较两个列表的各种方法。


原创文章,作者:MZXU9,如若转载,请注明出处:https://www.506064.com/n/130532.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
MZXU9MZXU9
上一篇 2024-10-03 23:28
下一篇 2024-10-03 23:28

相关推荐

  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • Python字符串宽度不限制怎么打代码

    本文将为大家详细介绍Python字符串宽度不限制时如何打代码的几个方面。 一、保持代码风格的统一 在Python字符串宽度不限制的情况下,我们可以写出很长很长的一行代码。但是,为了…

    编程 2025-04-29
  • 如何在PyCharm中安装OpenCV?

    本文将从以下几个方面详细介绍如何在PyCharm中安装OpenCV。 一、安装Python 在安装OpenCV之前,请确保已经安装了Python。 如果您还没有安装Python,可…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29

发表回复

登录后才能评论