def sort_and_pop(x: list, i: int) -> list:
x.sort()
return x.pop(i)
我的代码
lst = [23, 17, 3, 13, 11, 5, 7, 2, 19, 1]
lst = sort_and_pop(lst, 5)
lst = sort_and_pop(lst, 2)
代码运行显示的错误
Traceback (most recent call last):
File "<pyshell#67>", line 1, in
lst = sort_and_pop(lst, 2)
File "/Users/stealthemoonwithme/Desktop/nbm.py", line 3, in sort_and_pop
x.sort()
AttributeError: 'int' object has no attribute 'sort'
请问该如何修改
第一次调用 pop返回数组项为数字,第二次在调用lst是数字,不是数组当然没有sort方法了,应该是return pop后的x参数
def sort_and_pop(x: list, i: int) -> list:
x.sort()
x.pop(i)
return x
lst = [23, 17, 3, 13, 11, 5, 7, 2, 19, 1]
lst = sort_and_pop(lst, 5)
print(lst)
lst = sort_and_pop(lst, 2)
print(lst)