【发布时间】: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