小编Out*_*axy的帖子

Python 代码与 if 的比较验证 False 当它应该是 True 时

我在下面有以下 Python 代码。我期待代码返回,True但是当我运行它时,它似乎总是返回False. 检查 361 是否为 361 时似乎失败,但我不知道为什么:

def comp(array1, array2):
    if array1 is None or array2 is None or len(array1) is 0 or len(array2) is 0 or len(array1) is not len(array2):
        return False

    aListSquared = [x * x for x in sorted(array1)]
    array2.sort()

    print(aListSquared)
    print(array2)

    for x in range(len(aListSquared)):
        print('{0}:{1}'.format(aListSquared[x], type(aListSquared[x])))
        print('{0}:{1}'.format(array2[x], type(array2[x])))

        if int(aListSquared[x]) is not int(array2[x]):
            return False

    return True


a1 = [121, 144, 19, 161, 19, 144, 19, 11]
a2 = [11 …
Run Code Online (Sandbox Code Playgroud)

python integer comparison-operators

0
推荐指数
1
解决办法
88
查看次数

标签 统计

comparison-operators ×1

integer ×1

python ×1