第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我需要一些關(guān)于 Python 列表方法的幫助,存儲系統(tǒng)自動給出的輸入并在調(diào)用時執(zhí)行

我需要一些關(guān)于 Python 列表方法的幫助,存儲系統(tǒng)自動給出的輸入并在調(diào)用時執(zhí)行

動漫人物 2022-12-27 16:48:51
所以我需要存儲來自系統(tǒng)的輸入,并在通過輸入給出的命令與條件匹配時觸發(fā)代碼塊。給定的命令是由系統(tǒng)隨機產(chǎn)生的,每次執(zhí)行代碼時都不相同。我在下面做的是;我將輸入存儲在一個列表中,直到輸入變成空格,這表明命令已經(jīng)結(jié)束,并且在聲明中特別說明命令將在最后一個命令之后以空格結(jié)束。從該命令列表中讀取命令、輸入和值,直到?jīng)]有要執(zhí)行的命令為止。我知道這是不好的做法。由于我是這種語言的新手,我需要一些建議來更改我的代碼。提前致謝。順便說一句,我無法更改 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條經(jīng)驗 獲得超4個贊

您可以通過在字典中定義要執(zhí)行的操作、將輸入的值添加為拆分列表并為適當?shù)妮斎胝{(diào)用適當?shù)暮瘮?shù)來改進:


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 關(guān)注
  • 77 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號