简体   繁体   English

我有两个几乎相同的“for循环”函数,但只有一个有效。 这是为什么?

[英]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 它实际上从来没有足够的比较34

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.

相关问题 为什么当我在程序中使用两个功能时,只有一个可以工作 - Why when I use two functions in a program only one works PyQt4按钮,但只有一个可用。 为什么? - PyQt4 Buttons but only one works. Why? python df.melt - 两个相同的语句,但只有一个有效? - python df.melt - two identical statements, but only one of them works? 循环中的两个函数,但只执行一个(DICOM 文件的匿名化 + 移动它们) - Two functions in a loop, but only one is executed (anonymizing of DICOM files + moving them) re.search()和群组无法以与正常格式相同的文件格式工作。 蟒蛇 - re.search() & group not working in file with identical format to one that works. Python 我可以在python中将这两个几乎相同的函数合二为一吗? - Can I turn these two almost identical functions into one in python? 为什么我的登录功能之一有效而另一个无效,尽管它们都具有相同的代码? - Why does one of my login functions work and the other does not, despite the fact that they both have the identical code? 仅在我两个相同的网站之一上出现ImportError - ImportError on only one of my two identical sites 我写了两个单独的文件,一个包含类,另一个调用它们,但调用一个文件构成了所有文件的对象。 为什么? - I have written two separate files, one containing classes and the other calling them but calling one makes object of all. Why? 4个功能几乎相同,一个可以工作,但其他功能都没有错误 - 4 functions near identical, one works but the others dont no errors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM