Python 如何检查两个元组列表是否相同
我需要检查元组列表是否按元组的第一个属性排序。起初,我想对照这个列表的排序结果来检查它。比如Python 如何检查两个元组列表是否相同,python,arrays,list,sorting,tuples,Python,Arrays,List,Sorting,Tuples,我需要检查元组列表是否按元组的第一个属性排序。起初,我想对照这个列表的排序结果来检查它。比如 list1 = [(1, 2), (4, 6), (3, 10)] sortedlist1 = sorted(list1, reverse=True) 然后如何检查列表1是否与sortedlist1相同?相同,如list1[0]==sortedlist1[0]和list1[1]==sortedlist1[1]中所示 列表的长度可能为5或100,因此执行list1[0]==sortedlist1[0]和
list1 = [(1, 2), (4, 6), (3, 10)]
sortedlist1 = sorted(list1, reverse=True)
然后如何检查列表1是否与sortedlist1相同?相同,如list1[0]==sortedlist1[0]和list1[1]==sortedlist1[1]
中所示
列表的长度可能为5或100,因此执行list1[0]==sortedlist1[0]和list1[1]==sortedlist1[1]
将不是一个选项,因为我不确定列表的长度。
谢谢我相信你可以只做
list1==sortedlist1
,而不必逐个检查每个元素。我相信你可以只做list1==sortedlist1
,而不必逐个检查每个元素。如果你想检查列表是否已排序,想到一个非常简单的解决方案:
last_elem, is_sorted = None, True
for elem in mylist:
if last_elem is not None:
if elem[0] < last_elem[0]:
is_sorted = False
break
last_elem = elem
如果要检查列表是否已排序,则会想到一个非常简单的解决方案:
last_elem, is_sorted = None, True
for elem in mylist:
if last_elem is not None:
if elem[0] < last_elem[0]:
is_sorted = False
break
last_elem = elem
@joce已经提供了(我建议接受这一条,因为它更简洁、更直接地回答了您的问题),但我想谈谈您原始帖子的这一部分:
列表的长度可能为5或100,因此执行list1[0]==sortedlist1[0]和list1[1]==sortedlist1[1]
将不是一个选项,因为我不确定列表的长度
如果要比较两个列表的每个元素,则不需要确切知道列表的长度。编程就是懒惰,所以你可以打赌没有一个好的程序员会手工写出这么多的比较
相反,我们可以使用索引遍历这两个列表。这将允许我们同时对两个列表中的每个元素执行操作。下面是一个例子:
def compare_lists(list1, list2):
# Let's initialize our index to the first element
# in any list: element #0.
i = 0
# And now we walk through the lists. We have to be
# careful that we do not walk outside the lists,
# though...
while i < len(list1) and i < len(list2):
if list1[i] != list2[i]:
# If any two elements are not equal, say so.
return False
# We made it all the way through at least one list.
# However, they may have been different lengths. We
# should check that the index is at the end of both
# lists.
if i != (len(list1) - 1) or i != (len(list2) - 2):
# The index is not at the end of one of the lists.
return False
# At this point we know two things:
# 1. Each element we compared was equal.
# 2. The index is at the end of both lists.
# Therefore, we compared every element of both lists
# and they were equal. So we can safely say the lists
# are in fact equal.
return True
@joce已经提供了(我建议接受这一条,因为它更简洁、更直接地回答了您的问题),但我想谈谈您原始帖子的这一部分:
列表的长度可能为5或100,因此执行list1[0]==sortedlist1[0]和list1[1]==sortedlist1[1]
将不是一个选项,因为我不确定列表的长度
如果要比较两个列表的每个元素,则不需要确切知道列表的长度。编程就是懒惰,所以你可以打赌没有一个好的程序员会手工写出这么多的比较
相反,我们可以使用索引遍历这两个列表。这将允许我们同时对两个列表中的每个元素执行操作。下面是一个例子:
def compare_lists(list1, list2):
# Let's initialize our index to the first element
# in any list: element #0.
i = 0
# And now we walk through the lists. We have to be
# careful that we do not walk outside the lists,
# though...
while i < len(list1) and i < len(list2):
if list1[i] != list2[i]:
# If any two elements are not equal, say so.
return False
# We made it all the way through at least one list.
# However, they may have been different lengths. We
# should check that the index is at the end of both
# lists.
if i != (len(list1) - 1) or i != (len(list2) - 2):
# The index is not at the end of one of the lists.
return False
# At this point we know two things:
# 1. Each element we compared was equal.
# 2. The index is at the end of both lists.
# Therefore, we compared every element of both lists
# and they were equal. So we can safely say the lists
# are in fact equal.
return True
在Python3.x中,您可以检查两个元组列表
a
和b
使用eq
运算符相等
import operator
a = [(1,2),(3,4)]
b = [(3,4),(1,2)]
# convert both lists to sets before calling the eq function
print(operator.eq(set(a),set(b))) #True
在Python3.x中,您可以检查两个元组列表
a
和b
使用eq
运算符相等
import operator
a = [(1,2),(3,4)]
b = [(3,4),(1,2)]
# convert both lists to sets before calling the eq function
print(operator.eq(set(a),set(b))) #True
使用以下命令:
sorted(list1) == sorted(list2)
使用以下命令:
sorted(list1) == sorted(list2)
这是正确的,因为类似类型的序列支持迭代词典比较。见表格后的注释。因此,相等比较在步骤中迭代序列,直到到达不同的对或序列的结尾(在前一种情况下返回
False
,在后一种情况下返回True
)。这是正确的,因为类似类型的序列支持迭代字典比较。见表格后的注释。因此,相等比较在步骤中迭代序列,直到到达不同的对或序列的结尾(在前一种情况下返回False
,在后一种情况下返回True
)。