为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

动漫人物 2022-12-27 16:48:51
所以我需要存储来自系统的输入,并在通过输入给出的命令与条件匹配时触发代码块。给定的命令是由系统随机产生的,每次执行代码时都不相同。我在下面做的是;我将输入存储在一个列表中,直到输入变成空格,这表明命令已经结束,并且在声明中特别说明命令将在最后一个命令之后以空格结束。从该命令列表中读取命令、输入和值,直到没有要执行的命令为止。我知道这是不好的做法。由于我是这种语言的新手,我需要一些建议来更改我的代码。提前致谢。顺便说一句,我无法更改 if 语句中的条件,因为通过输入给定的命令不一样,但像这样以及更多:追加 15插入它 0 25remove_it 30代码工作得很好我需要建议,使它成为良好的代码实践,以提高我在 Python 中的水平。i = 0command_list = []while True:    command = input('')    if command == '':        break    command_list.append(command)    i += 1b = 0arr = []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()
查看完整描述

1 回答

?
绝地无双

TA贡献1946条经验 获得超4个赞

您可以通过在字典中定义要执行的操作、将输入的值添加为拆分列表并为适当的输入调用适当的函数来改进:


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 test


def reverseit(a):    a.reverse()    

def sortit(a):       a.sort()    

def popit(a):        a.pop()



# define what command to run for what input

cmds = {"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 42

append_it 32

append_it 52

append_it 62

append_it 82

append_it 12

append_it 22

append_it 33

append_it 12

print_it            # 1st printout

sort_it

print_it            # 2nd printout sorted

reverse_it

print_it            # 3rd printout reversed sorted

pop_it              

print_it            # one elem popped

insert_it 4 99

remove_it 42

print_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]


查看完整回答
反对 回复 2022-12-27
  • 1 回答
  • 0 关注
  • 48 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信