【问题标题】:Python: else statement returns True, though it's mentioned to return FalsePython:else 语句返回 True,尽管提到它返回 False
【发布时间】:2019-10-03 00:32:06
【问题描述】:

我正在编写一个函数,它验证作为参数传递的两个列表,如果两个列表的结构相同则返回 True,否则返回 False。

尝试在代码的多个位置使用 print 语句,没有发现问题。对于不同的结构列表,Else 语句按预期打印“False”,但奇怪的是函数返回 True,尽管它应该返回 False。

def same_structure_as(original,other):
    if isinstance(original, list) and isinstance(other, list):
        if len(original) == len(other):
            for el in original:
                if isinstance(el, list):
                    orig_new = original[original.index(el)]
                    other_new = other[original.index(el)]
                    same_structure_as(orig_new,other_new)
            return True
        else:
            return False
    else:
        print("False")
        return False

same_structure_as([1,[1,1]],[[2,2],2])

由于两个输入列表的结构不同,代码应该返回 False。 print 语句正确打印“False”,但返回“True”,即使我给出了“return False”

【问题讨论】:

  • @Prune 它打印 False,但返回 True。在 shell 而不是文件中完成,您会看到这两个值。

标签: python python-3.x


【解决方案1】:

如果内部列表不匹配,则不会返回 False:

def same_structure_as(original,other):
    if isinstance(original, list) and isinstance(other, list):
        if len(original) == len(other):
            for el in original:
                if isinstance(el, list):
                    orig_new = original[original.index(el)]
                    other_new = other[original.index(el)]
                    same_structure_as(orig_new,other_new) # here - the result is ignored
            return True
        else:
            return False
    else:
        print("False")
        return False

你需要

def same_structure_as(original,other):
    if isinstance(original, list) and isinstance(other, list):
        if len(original) == len(other):
            for el in original:
                if isinstance(el, list):
                    orig_new = original[original.index(el)]
                    other_new = other[original.index(el)]
                    if not same_structure_as(orig_new,other_new): # early exit if not same 
                        return False
                     # else continue testing (no else needed - simply not return anything)
            return True
        else:
            return False
    else:
        print("False")
        return False

否则你“检测/打印”错误但永远不会采取行动。

【讨论】:

    【解决方案2】:

    修复它的最简单方法是简单地 return 你的递归函数(我确定这就是你的意图):

    def same_structure_as(original, other):
        if isinstance(original, list) and isinstance(other, list):
            if len(original) == len(other):
                for el in original:
                    if isinstance(el, list):
                        orig_new = original[original.index(el)]
                        other_new = other[original.index(el)]
                        return same_structure_as(orig_new, other_new) # just add a return here
                return True
            else:
                return False
        else:
            print("False")
            return False
    
    print(same_structure_as([1,[1,1]],[[2,2],2]))
    

    产生正确的:

    错误

    错误

    drew a diagram在之前的帖子中解释了这种情况

    【讨论】:

    • @SaranyaRaja 很高兴听到!如果这解决了您的问题,标准的做法是单击帖子旁边的绿色复选标记以接受它作为解决方案(如果出现更好的答案,您也可以更改此设置),因为这向海报表明问题已解决并关闭线程。当您获得 15 个代表时,您也可以对有用的问题/答案进行投票。欢迎使用 StackOverflow!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 2022-01-15
    • 2021-09-01
    • 1970-01-01
    • 2020-05-28
    • 1970-01-01
    • 2020-08-20
    相关资源
    最近更新 更多