python 两个列表比较_如何在Python中比较两个列表

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中两个列表的不同方法 我们经常遇到需要比较存储在任何结构(例如listtuplestring等)中的数据项的值的情况。

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() function

    reduce()和map()函数
  • The collection.counter() function

    collection.counter()函数
  • Python sort() function along with == operator

    Python sort()函数以及==运算符
  • Python set() function along with == operator

    Python set()函数以及==运算符
  • The difference() function

    Difference()函数


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 两个列表比较

  • 7
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值