python 两个列表比较

In this article, we will understand the different ways to compare two lists in Python. We often come across situations wherein we need to compare the values of the data items stored in any structure say list, tuple, string, etc.

在本文中,我们将了解比较Python中两个列表的不同方法 我们经常遇到需要比较存储在任何结构(例如list , tuple , string等)中的数据项的值的情况。

Comparison is the method of checking the data items of a list against equality with the data items of another list.

Comparison是一种检查列表中的数据项与另一个列表中的数据项是否相等的方法。



在Python中比较两个列表的方法 (Methods to Compare Two Lists in Python)

We can use either of the following methods to perform our comparison:

我们可以使用以下两种方法之一进行比较:

  • The reduce() and map() functionreduce()和map()函数
  • The collection.counter() functioncollection.counter()函数
  • Python sort() function along with == operatorPython sort()函数以及==运算符
  • Python set() function along with == operatorPython set()函数以及==运算符
  • The difference() functionDifference()函数


1. Python reduce()和map()函数 (1. Python reduce() and map() functions )

We can use the Python map() function along with functools.reduce() function to compare the data items of two lists.

我们可以将Python map()函数 与functools.reduce ()函数 一起使用来比较两个列表的数据项。

The map() method accepts a function and an iterable such as list, tuple, string, etc. as arguments.

map()方法接受一个函数和一个可迭代 函数 (例如list,tuple,string等)作为参数

It applies the passed function to each item of the iterable and then returns a map object i.e. an iterator as the result.

它将传递的函数应用于可迭代的每个项,然后返回一个映射对象,即迭代器作为结果

The functools.reduce() method applies the passed function to every element of the input iterable in a recursive manner.

functools.reduce()方法以递归方式将传递的函数应用于输入中可迭代的每个元素。

Initially, it would apply the function on the first and the second element and returns the result. The same process will continue on each of the elements until the list has no elements left.

最初,它将在第一个和第二个元素上应用该函数并返回结果。 每个元素将继续相同的过程,直到列表中没有剩余的元素。

As a combination, the map() function would apply the input function to every element and the reduce() function will make sure that it applies the function in a consecutive manner.

作为一种组合,map()函数会将输入函数应用于每个元素,而reduce()函数将确保以连续的方式应用该函数。

Example:

例:


import functools l1 = [10, 20, 30, 40, 50]
l2 = [10, 20, 30, 50, 40, 70]
l3 = [10, 20, 30, 40, 50] if functools.reduce(lambda x, y : x and y, map(lambda p, q: p == q,l1,l2), True): print ("The lists l1 and l2 are the same")
else: print ("The lists l1 and l2 are not the same") if functools.reduce(lambda x, y : x and y, map(lambda p, q: p == q,l1,l3), True): print ("The lists l1 and l3 are the same")
else: print ("The lists l1 and l3 are not the same") 

Output:

输出:


The lists l1 and l2 are not the same
The lists l1 and l3 are the same


2. Python collection.counter()方法 (2. Python collection.counter() method)

The collection.counter() method can be used to compare lists efficiently. The counter() function counts the frequency of the items in a list and stores the data as a dictionary in the format <value>:<frequency>.

collection.counter()方法可用于有效地比较列表。 counter()函数对列表中项目的频率进行计数,并将数据作为字典存储,格式为<value>:<frequency>

If two lists have the exact same dictionary output, we can infer that the lists are the same.

如果两个列表具有完全相同的字典输出,我们可以推断出列表是相同的。

Note: The list order has no effect on the counter() method.

注意:列表顺序对counter()方法没有影响。

Example:

例:


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

Output:

输出:


The lists l1 and l2 are not the same
The lists l1 and l3 are the same


3. Python sort()方法和==运算符以比较列表 (3. Python sort() method and == operator to compare lists)

We can club the Python sort() method with the == operator to compare two lists.

我们可以将Python sort()方法与==运算符结合使用,以比较两个列表。

Python sort() method is used to sort the input lists with a purpose that if the two input lists are equal, then the elements would reside at the same index positions.

Python sort()方法用于对输入列表进行排序,目的是如果两个输入列表相等,则元素将位于相同的索引位置

Note: The order of the list does not affect this method because we’ll be sorting the lists before comparison.

注意:列表的顺序不会影响此方法,因为我们将在比较之前对列表进行排序。

Further, the == operator is used to compare the list, element by element.

此外, == operator用于逐元素比较列表。

Example:

例:


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

Output:

输出:


The lists l1 and l3 are the same
The lists l1 and l2 are not the same


4. Python set()方法和==运算符比较两个列表 (4. Python set() method and == operator to compare two lists)

Python set() method manipulates the data items of an iterable to a sorted sequence set of data items without taking the order of elements into consideration.

Python set()方法可将可迭代的数据项处理为数据项的排序序列集, 而无需考虑元素的顺序

Further, the == operator is used for comparison of the data items of the list in an element-wise fashion.

此外, == operator用于按元素方式比较列表的数据项。

Example:

例:


l1 = [10, 20, 30, 40, 50]
l3 = [50, 10, 30, 20, 40] a = set(l1)
b = set(l3)if a == b:print("Lists l1 and l3 are equal")
else:print("Lists l1 and l3 are not equal")

Output:

输出:


Lists l1 and l3 are equal


5. Python自定义列表理解以比较两个列表 (5. Python Custom List Comprehension to Compare Two Lists)

We can use Python List comprehension to compare two lists.

我们可以使用Python List理解来比较两个列表。

Example:

例:


l1 = [10, 20, 30, 40, 50]
l3 = [50, 75, 30, 20, 40, 69] res = [x for x in l1 + l3 if x not in l1 or x not in l3]print(res)
if not res:print("Lists l1 and l3 are equal")
else:print("Lists l1 and l3 are not equal")

In the above code, we set a pointer element ‘x’ to the list l1 and l3. Further, we check if the element pointed by the pointer element is present in the lists.

在上面的代码中,我们将指针元素“ x”设置为列表l1和l3。 此外,我们检查指针元素所指向的元素是否存在于列表中。

Output:

输出:


[10, 75, 69]
Lists l1 and l3 are not equal


结论 (Conclusion)

Thus, in this article, we have covered and understood a number of ways to compare multiple lists in Python.

因此,在本文中,我们涵盖并理解了多种比较Python中多个列表的方法。

翻译自: https://www.journaldev.com/37089/how-to-compare-two-lists-in-python

python 两个列表比较

python 两个列表比较_如何在Python中比较两个列表相关推荐

  1. java 两个字段排序_如何在Java中按两个字段排序?

    使用Java 8流方法..//Creates and sorts a stream (does not sort the original list) persons.stream().sorted( ...

  2. java 合并两个列表_如何在Java中合并两个列表?

    java 合并两个列表 Merging two lists in Java is often a useful operation. These lists can be ArrayLists or ...

  3. 有两个python怎么停用其中一_如何在python中停止另一个已经运行的脚本?

    There is a way to start another script in python by doing this: import os os.system("python [na ...

  4. python 参数个数 同名函数_如何在python中编写不同参数的同名方法

    我在Java背景下学习Python(3.x). 我有一个python程序,我在其中创建一个personObject并将其添加到列表中.p = Person("John") list ...

  5. python画图修改背景颜色_如何在 Matplotlib 中更改绘图背景的实现

    介绍 Matplotlib是Python中使用最广泛的数据可视化库之一.无论是简单还是复杂的可视化项目,它都是大多数人的首选库. 在本教程中,我们将研究如何在Matplotlib中更改绘图的背景. 导 ...

  6. python怎么去掉换行符_如何在Python中删除尾部换行符?

    如何在Python中删除尾部换行符? 什么是Perl的chomp函数的Python等价物,如果它是换行符,它会删除字符串的最后一个字符? 26个解决方案 1473 votes 尝试方法lstrip() ...

  7. 用python画奔驰的标志_如何在CATIA中快速画一个奔驰车标

    原标题:如何在CATIA中快速画一个奔驰车标 咱们这个公众号呀,总是发一些二次开发啊,代码啊什么的,这观众看的啊,是云里雾里的!哎,内位说了:您能不能讲点儿我们听的懂的内容啊?那好,今儿咱们就来说说, ...

  8. python词云自定义形状_如何在Python中生成任何形状的词云

    作者 | Julia Kho 编辑| 代码医生团队 在本文中,我们将探讨如何在python中以您想要的任何形状生成文字云.我们将通过一个示例来说明如何在房屋的自定义形状中创建简单的文字云,如上图所示. ...

  9. python实例化对象做实参_如何在Python中记住类实例化?

    好的,这是真实的场景:我正在编写一个应用程序,我有一个类,它表示某种类型的文件(在我的例子中,这是照片,但细节与问题无关).照片类的每个实例对于照片的文件名都应该是唯一的. 问题是,当用户告诉我的应用 ...

最新文章

  1. linux qt小型计算器,Qt实现一个简单的计算器
  2. ipguard客户端如何卸载_客户端navicat遇到问题怎么办?
  3. 关于TCP/IP与数据传输
  4. phpsduty环境下,使用composer安装报错
  5. OSPF-1-OSPF的数据库交换(4)
  6. WCF 第十二章 对等网 System.Net.PeerToPeer.Collaboration
  7. Typedef声明简介
  8. UNIX 环境高级编程(六)—— 程序和进程
  9. 云队友丨巴菲特是怎样炼成的?两万字长文深度起底股神的传奇人生
  10. ConfuserEx 脱壳软件 使用教程
  11. 经济学原理上中国故事2019尔雅满分答案
  12. python数据分析:客户价值分析案例实战
  13. 论文阅读|《面向多目标柔性作业车间调度的强化学习NSGA-Ⅱ算法》
  14. 偶现BUG的处理方式
  15. MathRound修约
  16. 实验2 运算器的编程实现
  17. C语言中取整数的几种方法
  18. JAVA毕设项目社区生鲜电商平台(java+VUE+Mybatis+Maven+Mysql)
  19. 各知名企业笔试题笔经大全
  20. 使用WebService进行网络编程【工具类】

热门文章

  1. 分布式数据库架构及企业实践--基于Mycat中间件pdf
  2. 面向对象3-析构函数和私有属性
  3. java第一次作业0
  4. 在网页中嵌入百度地图的步骤
  5. 楼市信贷新政力度超市场预期 房企放风要涨价
  6. 很多人搞不清楚的两个类Vector,ArrayList
  7. [转载] java clone方法_Java Calendar clone()方法与示例
  8. Python 基础课程第五天
  9. Codeforces.612E.Square Root of Permutation(构造)
  10. 关于vue的npm run dev和npm run build