我需要一些关于 Python 列表方法的帮助,存储系统自动给出的输入并在调用时执行

所以我需要存储来自系统的输入,并在通过输入给出的命令与条件匹配时触发代码块。给定的命令是由系统随机产生的,每次执行代码时都不相同。我在下面做的是;我将输入存储在一个列表中,直到输入变成空格,这表明命令已经结束,并且在声明中特别说明命令将在最后一个命令之后以空格结束。从该命令列表中读取命令、输入和值,直到没有要执行的命令为止。我知道这是不好的做法。由于我是这种语言的新手,我需要一些建议来更改我的代码。提前致谢。顺便说一句,我无法更改 if 语句中的条件,因为通过输入给定的命令不一样,但像这样以及更多:

追加 15

插入它 0 25

remove_it 30

代码工作得很好我需要建议,使它成为良好的代码实践,以提高我在 Python 中的水平。

i = 0

command_list = []

while True:

    command = input('')

    if command == '':

        break

    command_list.append(command)

    i += 1


b = 0

arr = []

while i != b:


    command1 = command_list[b]

    b += 1


    if command1[0:8] == "append_it":

        value = int(command1[9:])

        arr.append(value)


    elif command1[0:4] == "insert_it":

        index = int(command1[5:7])

        value = int(command1[7:])

        arr.insert(index, value)


    elif command1[0:3] == "remove_it":

        value = int(command1[4:])

        if value in liste:

            arr.remove(value)


    elif command1[0:] == "print_it":

        print(arr)


    elif command1[0:] == "reverse_it":

        arr.reverse()


    elif command1[0:] == "sort_it":

        arr.sort()


    elif command1[0:] == "pop_it":

        arr.pop()


动漫人物
浏览 50回答 1
1回答

绝地无双

您可以通过在字典中定义要执行的操作、将输入的值添加为拆分列表并为适当的输入调用适当的函数来改进:def appendit(a, *prms):    v = int(prms[0])    a.append(v)def insertit(a, *prms):    i = int(prms[0])    v = int(prms[1])    a.insert(i,v)def removeit(a, *prms):     v = int(prms[0])    a.remove(v) # no need to testdef reverseit(a):    a.reverse()    def sortit(a):       a.sort()    def popit(a):        a.pop()# define what command to run for what inputcmds = {"append_it" : appendit,         "insert_it" : insertit,         "remove_it" : removeit,         "print_it"  : print,        # does not need any special function        "reverse_it": reverseit,         "sort_it"   : sortit,         "pop_it"    : popit}command_list = []while True:    command = input('')     if command == '':        break    c = command.split()   # split the command already    # only allow commands you know into your list - they still might have the    # wrong amount of params given - you should check that in the functions     if c[0] in cmds:        command_list.append(c)arr = []for (command, *prms) in command_list:    # call the correct function with/without params    if prms:        cmds[command](arr, *prms)    else:        cmds[command](arr)输出:# inputs from user:append_it 42append_it 32append_it 52append_it 62append_it 82append_it 12append_it 22append_it 33append_it 12print_it            # 1st printoutsort_itprint_it            # 2nd printout sortedreverse_itprint_it            # 3rd printout reversed sortedpop_it              print_it            # one elem poppedinsert_it 4 99remove_it 42print_it            # 99 inserted and 42 removed# print_it - outputs[42, 32, 52, 62, 82, 12, 22, 33, 12][12, 12, 22, 32, 33, 42, 52, 62, 82][82, 62, 52, 42, 33, 32, 22, 12, 12][82, 62, 52, 42, 33, 32, 22, 12][82, 62, 52, 99, 33, 32, 22, 12]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python