[英]I have two practically identical “for loop” functions, but only one of them works. Why is that?
I'm learning loops in Python and I'm trying to make a function that takes 2 lists of the same length and returns True if lst1 is the same as lst2 reversed (and false otherwise). 我正在学习Python中的循环,我正在尝试创建一个函数,它接受2个相同长度的列表,如果lst1与lst2反向相同则返回True(否则返回false)。
For example: reversed_list([1, 2, 3], [3, 2, 1]) should return True. 例如:reversed_list([1,2,3],[3,2,1])应返回True。
Below there are 2 nearly identical ways of writing this function, I think they should both work correctly but the second one doesn't work even though it is practically identical to the first. 下面有两种几乎相同的写这个功能的方法,我认为它们应该都能正常工作,但第二个不能正常工作,即使它与第一个实际上完全相同。
def reversed_list(lst1, lst2):
for index in range(len(lst1)):
if lst1[index] != lst2[len(lst2) - 1 - index]:
return False
else: return True
#tests
print(reversed_list([1, 2, 3], [3, 2, 1]))
#output = True (correct)
print(reversed_list([1, 2, 3], [4, 2, 1]))
#output = False (correct)
def reversed_list(lst1, lst2):
for index in range(len(lst1)):
if lst1[index] == lst2[len(lst2) - 1 - index]:
return True
else: return False
#tests
print(reversed_list([1, 2, 3], [3, 2, 1]))
#output = True (correct but it outputs True even if it should be False)
print(reversed_list([1, 2, 3], [4, 2, 1]))
#output = True (incorrect)
The only difference between the two functions is that I changed != to == and swapped True/False accordingly. 两个函数之间的唯一区别是我改变了!= to ==并相应地交换了True / False。 However, the second function always returns True even if it should be False.
但是,第二个函数总是返回True,即使它应该为False。 I suspect this has something to do with the loop.
我怀疑这与循环有关。 Please explain why the second function doesn't work properly.
请解释为什么第二个功能无法正常工作。
The first one stops early and returns False
if it finds two entries that don't match; 第一个停止提前,如果找到两个不匹配的条目,则返回
False
; otherwise it returns True
. 否则返回
True
。 The second one stops early and returns True
as soon as it finds two entries that do match - but your task is to verify all the entries, while this stops after verifying only one. 第二个提前停止并在找到两个匹配的条目时立即返回
True
- 但您的任务是验证所有条目,而在仅验证一个条目后停止。 It's basically checking whether there's at least one match, rather than checking whether the whole list is a match. 它基本上检查是否至少有一个匹配,而不是检查整个列表是否匹配。
The problem with the second function is that the function will stop running as soon as it finds a match, rather than check all the values and stop running if finds a pair that doesn't match. 第二个函数的问题是函数一旦找到匹配就会停止运行,而不是检查所有值并在找到不匹配的对时停止运行。
In your case of [1, 2, 3]
vs [4, 2, 1]
在
[1, 2, 3]
vs [4, 2, 1]
4,2,1 [4, 2, 1]
When these two lines run: 当这两行运行时:
if lst1[index] == lst2[len(lst2) - 1 - index]:
return True
The second function got to the first pair of items when index == 0
and saw that lst[0]
has a value of 1
and that lst2[len(lst2) - 1 - 0]
also has a value of 1
. 当
index == 0
时,第二个函数到达第一对项,并且看到lst[0]
的值为1
,而lst2[len(lst2) - 1 - 0]
的值也为1
。 They do match, so the function returns true. 它们匹配,因此函数返回true。 It never actually makes it far enough to compare
3
vs 4
它实际上从来没有足够的比较
3
比4
Look into what is called a fail fast algorithm if you want to know more about this. 如果您想了解更多相关信息,请查看所谓的失败快速算法 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.