此 Python 程式碼旨在根據使用者提供的命令對清單執行一系列操作。讓我們一步步分析程式碼,了解其工作原理:
if __name__ == '__main__': N = int(input()) l = [] while(N>0): cmd_l = input().split() if(len(cmd_l) == 3 and cmd_l[0] == "insert"): #insert statement l.insert(int(cmd_l[1]),int(cmd_l[2])) elif(len(cmd_l) == 2 and (cmd_l[0] == "remove" or cmd_l[0] == "append")): if(cmd_l[0] == "remove"): l.remove(int(cmd_l[1])) elif(cmd_l[0] == "append"): l.append(int(cmd_l[1])) elif(len(cmd_l) == 1): if(cmd_l[0] == "sort"): l.sort() elif(cmd_l[0] == "reverse"): l.reverse() elif(cmd_l[0] == "pop"): l.pop() elif(cmd_l[0] == "print"): print(l) N -= 1
如果 __name__ == '__main__':
N = int(輸入())
l = []
同時(N>0):
cmd_l = input().split()
if(len(cmd_l) == 3 且 cmd_l[0] == "插入"):
l.insert(int(cmd_l[1]),int(cmd_l[2]))
elif(len(cmd_l) == 2 and (cmd_l[0] == "刪除" 或 cmd_l[0] == "追加")):
if(cmd_l[0] == "刪除"):
l.remove(int(cmd_l[1]))
elif(cmd_l[0] == "追加"):
l.append(int(cmd_l[1]))
elif(len(cmd_l) == 1):
if(cmd_l[0] == "排序"):
l.sort()
elif(cmd_l[0] == "反向"):
l.reverse()
elif(cmd_l[0] == "pop"):
l.pop()
elif(cmd_l[0] == "列印"):
印(l)
N -= 1
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3