此 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(input())
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] == "print"):
打印(l)
N -= 1
以上是Python 中的 HackerRank 问题 - 基本数据类型列表的详细内容。更多信息请关注PHP中文网其他相关文章!