我的代码正在运行,但是我想从输出中删除“无”。我该怎么办?
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
答案 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}"