這是我的代碼,它給了我這個錯誤資訊。為什么?
if __name__ == '__main__':
l=[]
N = int(input())
for z in range(N):
x=input().split()
if (x[0]=="insert"):
l.insert(int(x[1]),int(x[2]))
elif(x[0]=="print"):
print(l)
elif(x[0]=="remove"):
l.remove(int(x[1]))
elif (x[0]=="append"):
l.append(x[1])
elif(x[0]=="sort"):
l.sort()
elif(x[0])=="pop":
l.pop()
elif(x[0]=="reverse"):
l.reverse()
錯誤訊息 - 回溯(最近一次呼叫最后一次):檔案“/tmp/submission/20220617/03/45/hackerrank-3495035b4042c8bc0c55e799a2d2e687/code/Solution.py”,第 15 行,在 l.sort() TypeError: '<' not在“str”和“int”的實體之間支持
uj5u.com熱心網友回復:
您在 處附加了字串值x[0] == "append"。
當您更改為時,它應該可以作業l.append(int(x[1])
uj5u.com熱心網友回復:
對不起。樣本輸入和輸出是。
樣本輸入:
12
insert 0 5
insert 1 10
insert 0 6
print
remove 6
append 9
append 1
sort
print
pop
reverse
print
輸出 :
[6, 5, 10]
[1, 5, 9, 10]
[9, 5, 1]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/492140.html
標籤:Python python-3.x 列表 排序 嵌套的
上一篇:Moq:驗證引數空參考中的物件
