def rotatelist(l,k):
n=len(l)
if k<0:
return(print(l))
k=k%n
b=[0]*n
for i in range(0,n):
b[(i+k)%n]=l[i]
return(print(b))
这是我的代码我写的旋转列表,停止打印打印列表后打印列表我没有使用返回,但它仍然在下一行打印无
答案 0 :(得分:0)
def rotatelist(l, k):
n = len(l)
if k < 0:
return(l)
k = k % n
b = [0] * n
for i in range(0, n):
b[(i + k) % n] = l[i]
return(b)
a = [1, 2, 3]
print(rotatelist(a, 2))
调用print()时总是返回None,即打印值并返回None,然后打印无
答案 1 :(得分:0)
您的函数不返回值b或l,而是返回print(b)和print(l)函数返回的内容(即None)。请尝试仅返回值:
def rotatelist(l, k):
n = len(l)
if k < 0:
return l
k = k % n
b = [0]*n
for i in range(0, n):
b[(i+k) % n] = l[i]
return b
a = [1, 2, 3, 4, 5, 6, 7]
print("start testing")
new_list = rotatelist(a, 3)
print("print result")
print(new_list)
print("testing over")
给出输出:
start testing
print result
[5, 6, 7, 1, 2, 3, 4]
testing over