介绍
当你编程或学习Python时,你可能需要确定两个或多个列表是否相等。当你比较列表以实现平等时,你正在检查列表是否相同长度,以及列表中的每个项目是否相等。
本文描述了如何使用以下Python功能来比较列表:
sort()
方法或sorted()
函数与==
运算符 *set()
函数使用==
运算符 *reduce()
和map()
函数使用==
运算符 *collection.Counter()
类与==
运算符 * 列表理解
使用 sort()
方法或 sorted()
函数来比较列表
您可以使用 sort()
方法或 sorted()
函数来对列表进行排序,以便进行对等比较。 sort()
方法对列表进行排序,而 sorted()
函数返回一个新的列表。
原始列表项目的顺序并不重要,因为列表在比较之前进行排序。
<$>[注] 注: 您只能对具有相同数据类型的项目的列表进行排序。
sort()
方法示例
下面的示例展示了如何使用 sort()
方法来对等式列表进行排序和比较:
1l1 = [10, 20, 30, 40, 50]
2l2 = [20, 30, 50, 40, 70]
3l3 = [50, 10, 30, 20, 40]
4
5l1.sort()
6l2.sort()
7l3.sort()
8
9if l1 == l2:
10 print ("The lists l1 and l2 are the same")
11else:
12 print ("The lists l1 and l2 are not the same")
13
14if l1 == l3:
15 print ("The lists l1 and l3 are the same")
16else:
17 print ("The lists l1 and l3 are not the same")
产量是:
1[secondary_label Output]
2The lists l1 and l3 are the same
3The lists l1 and l2 are not the same
上面的示例代码将每个列表分类,将l1
与l3
进行比较,并打印结果,然后将l1
与l2
进行比较,并打印结果。
sorted()
函数示例
下面的示例展示了如何使用 sorted()
函数来对等式列表进行排序和比较:
1l1 = [10, 20, 30, 40, 50]
2l2 = [20, 30, 50, 40, 70]
3l3 = [50, 10, 30, 20, 40]
4
5l1_sorted = sorted(l1)
6l2_sorted = sorted(l2)
7l3_sorted = sorted(l3)
8
9if l1_sorted == l2_sorted:
10 print ("The lists l1 and l2 are the same")
11else:
12 print ("The lists l1 and l2 are not the same")
13
14if l1_sorted == l3_sorted:
15 print ("The lists l1 and l3 are the same")
16else:
17 print ("The lists l1 and l3 are not the same")
产量是:
1[secondary_label Output]
2The lists l1 and l3 are the same
3The lists l1 and l2 are not the same
上面的示例代码返回每个列表的排序版本,将l1
与l3
进行比较,并打印结果,然后将l1
与l2
进行比较,并打印结果。
使用)`函数来比较列表
您可以使用Python map()
函数和 functools.reduce()
函数来比较两个列表的数据元素。
)`函数将给定的函数应用到迭代函数的每个项目,然后作为结果返回一个地图对象( iterator)。
「functools.reduce()」函数也接受函数和可迭代函数作为参数。「functools.reduce()」函数将该函数应用于可迭代函数的每一个元素。
当您将它们结合使用时,)`函数将确保它以连续的方式应用该函数。
列表项目的顺序在使用)函数时至关重要。以不同的顺序列出相同项目的列表在对比时不会返回
真`。
下面的示例展示了如何使用)`函数来比较对等的列表:
1import functools
2
3l1 = [10, 20, 30, 40, 50]
4l2 = [20, 30, 50, 40, 70]
5l3 = [10, 20, 30, 40, 50]
6
7if functools.reduce(lambda x, y : x and y, map(lambda p, q: p == q,l1,l2), True):
8 print ("The lists l1 and l2 are the same")
9else:
10 print ("The lists l1 and l2 are not the same")
11
12if functools.reduce(lambda x, y : x and y, map(lambda p, q: p == q,l1,l3), True):
13 print ("The lists l1 and l3 are the same")
14else:
15 print ("The lists l1 and l3 are not the same")
产量是:
1[secondary_label Output]
2The lists l1 and l2 are not the same
3The lists l1 and l3 are the same
上面的示例代码将l1
与l2
进行比较,然后将l1
与l3
进行比较。
使用 set()
函数来比较列表
您可以使用 set()
函数使用给定的列表创建 set
对象,然后使用 ==
运算符来比较对等的集合。
原始列表项目的顺序并不重要,因为在每个组包含任何顺序中的相同项目时,==
运算符返回true
。
<$>[注] 注: 重复列表项目只显示一次。
下面的示例展示了如何从列表创建集,并将集进行对等比较:
1l1 = [10, 20, 30, 40, 50]
2l2 = [50, 10, 30, 20, 40]
3
4a = set(l1)
5b = set(l2)
6
7if a == b:
8 print("Lists l1 and l2 are equal")
9else:
10 print("Lists l1 and l2 are not equal")
产量是:
1[secondary_label Output]
2Lists l1 and l2 are equal
上面的示例代码从列表l1
和l2
中创建a
和b
集,然后比较这些集并打印结果。
使用collections.Counter()
类来比较列表
可以使用 collections.Counter()
类来比较列表. counter()
函数计算列表中的项目的频率,并将数据存储为字典对象,以值:频率格式。
当您使用计数
类来比较列表时,原始列表项目的顺序并不重要。
下面的示例展示了如何从给定的列表中创建计数
对象,并将其比较为平等:
1import collections
2
3l1 = [10, 20, 30, 40, 50]
4l2 = [20, 30, 50, 40, 70]
5l3 = [50, 20, 30, 40, 10]
6
7if collections.Counter(l1) == collections.Counter(l2):
8 print ("The lists l1 and l2 are the same")
9else:
10 print ("The lists l1 and l2 are not the same")
11
12if collections.Counter(l1) == collections.Counter(l3):
13 print ("The lists l1 and l3 are the same")
14else:
15 print ("The lists l1 and l3 are not the same")
产量是:
1[secondary_label Output]
2The lists l1 and l2 are not the same
3The lists l1 and l3 are the same
上面的示例代码为列表l1
和l2
创建计数
对象,比较它们,并打印结果。
使用列表理解来比较列表
您可以使用列表理解来比较两个列表. 有关列表理解的更多信息,请参阅 理解列表理解在Python 3。
当您使用列表理解来比较列表时,原始列表项目的顺序并不重要。
下面的示例展示了如何使用列表理解来比较列表:
1l1 = [10, 20, 30, 40, 50]
2l2 = [50, 75, 30, 20, 40]
3l3 = [50, 20, 30, 40, 10]
4
5res = [x for x in l1 + l2 if x not in l1 or x not in l2]
6
7print(res)
8if not res:
9 print("Lists l1 and l2 are equal")
10else:
11 print("Lists l1 and l2 are not equal")
12
13res2 = [x for x in l1 + l3 if x not in l1 or x not in l3]
14
15print(res2)
16if not res2:
17 print("Lists l1 and l3 are equal")
18else:
19 print("Lists l1 and l3 are not equal")
上面的示例代码为列表l1
和l2
设置一个指针元素x
,然后检查指针元素指向的项目是否存在于列表中。
产量是:
1[secondary_label Output]
2[10, 75]
3Lists l1 and l2 are not equal
结论
本文描述了几个不同的方法来比较平等的名单在Python. 继续你的学习与更多 Python教程。