如何在Python中比较两个列表
在本文中,我们将了解比较Python中两个列表的不同方法。
我们经常遇到需要比较存储在列表,元组,字符串等任何结构中的数据项的值的情况。
比较是一种检查列表数据项与另一个列表数据项是否相等的方法。
在Python中比较两个列表的方法
我们可以使用以下两种方法之一进行比较:
- reduce()和map()函数
- collection.counter()函数
- Python sort()函数以及==运算符
- Python set()函数以及==运算符
- Difference()函数
1. Python reduce()和map()函数
我们可以将Python map()函数与functools.reduce()函数一起使用来比较两个列表的数据项。
map()方法接受一个函数和一个可迭代的函数,例如列表,元组,字符串等作为参数。
它将传递的函数应用于迭代器的每个项目,然后返回一个映射对象,即迭代器作为结果。
functools.reduce()
方法将传递的函数以递归方式应用于输入的每个可迭代元素。
最初,它将在第一个和第二个元素上应用该函数并返回结果。
每个元素将继续相同的过程,直到列表中没有剩余的元素。
作为一种组合,map()函数会将输入函数应用于每个元素,而reduce()函数将确保以连续的方式应用该函数。
例:
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")
输出:
The lists l1 and l2 are not the same The lists l1 and l3 are the same
2. Python collection.counter()方法
collection.counter()方法可用于有效地比较列表。
counter()函数计算列表中项目的频率,并将数据作为字典存储,格式为<value>:<frequency>。
如果两个列表具有完全相同的字典输出,我们可以推断出列表是相同的。
注意:列表顺序对counter()方法没有影响。
例:
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")
输出:
The lists l1 and l2 are not the same The lists l1 and l3 are the same
3. Python sort()方法和==运算符以比较列表
我们可以将Python sort()方法与==运算符结合使用,以比较两个列表。
Python的sort()方法用于对输入列表进行排序,目的是如果两个输入列表相等,则元素将位于相同的索引位置。
注意:列表的顺序不会影响此方法,因为我们将在比较之前对列表进行排序。
此外," =="运算符用于逐个元素比较列表。
例:
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")
输出:
The lists l1 and l3 are the same The lists l1 and l2 are not the same
4. Python set()方法和==运算符比较两个列表
Python set()方法可将可迭代的数据项处理为数据项的排序序列集,而无需考虑元素的顺序。
此外," ==运算符"用于按元素方式比较列表的数据项。
例:
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")
输出:
Lists l1 and l3 are equal
5. Python自定义列表理解以比较两个列表
我们可以使用Python List理解来比较两个列表。
例:
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")
在上面的代码中,我们将指针元素" x"设置到列表l1和l3。
此外,我们检查指针元素所指向的元素是否存在于列表中。
输出:
[10, 75, 69] Lists l1 and l3 are not equal