如何删除代码结果中出现的“无”?

时间:2019-11-03 15:11:25

标签: python

我的代码正在运行,但是我想从输出中删除“无”。我该怎么办?

def permutation(a,b):
    c = sorted(a)
    d = sorted(b)

    if c == d :
        return print(a," and ", b ," are permutations")
    else:
        count = 0
        for i in c:
            if c in d:
                d.remove(d.index(c))
            else:
                count = count + 1
        return print(a, " and ", b, " are NOT permutations; no. of differences = ", count)

print(permutation([10, 9, 11, 1] , [9,1,11,10] ))
print(permutation([10, 9, 1, 10] , [8,1,11,10]))

这是我的输出:

[10, 9, 11, 1]  and  [9, 1, 11, 10]  are permutations
None
[10, 9, 1, 10]  and  [8, 1, 11, 10]  are NOT permutations; no. of differences =  4
None

2 个答案:

答案 0 :(得分:2)

使用此功能

def permutation(a,b):
    c = sorted(a)
    d = sorted(b)

    if c == d :
        return (a," and ", b ," are permutations")
    else:
        count = 0
        for i in c:
            if c in d:
                d.remove(d.index(c))
            else:
                count = count + 1
        return (a, " and ", b, " are NOT permutations; no. of differences = ", count)
print(permutation([10, 9, 11, 1] , [9,1,11,10] ))
print(permutation([10, 9, 1, 10] , [8,1,11,10]))

函数[返回打印(某物)]无效

像这样

return print(a," and ", b ," are permutations")
 ||
 ||
\  /
 \/
return (a," and ", b ," are permutations")

答案 1 :(得分:1)

Print将值发送到程序标准输出,它不会尝试返回任何值。任何不返回值的函数或方法都将导致None被隐式返回。这就是None的来源(您将其返回到print(permutation(...)))。尝试返回格式化的字符串:

return f"{a} and {b} are permutations"
...
    return f"{a} and  {b} are NOT permutations; no. of differences = {count}"