此 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