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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

動(dòng)漫人物 2022-12-27 16:48:51
所以我需要存儲(chǔ)來(lái)自系統(tǒng)的輸入,并在通過(guò)輸入給出的命令與條件匹配時(shí)觸發(fā)代碼塊。給定的命令是由系統(tǒng)隨機(jī)產(chǎn)生的,每次執(zhí)行代碼時(shí)都不相同。我在下面做的是;我將輸入存儲(chǔ)在一個(gè)列表中,直到輸入變成空格,這表明命令已經(jīng)結(jié)束,并且在聲明中特別說(shuō)明命令將在最后一個(gè)命令之后以空格結(jié)束。從該命令列表中讀取命令、輸入和值,直到?jīng)]有要執(zhí)行的命令為止。我知道這是不好的做法。由于我是這種語(yǔ)言的新手,我需要一些建議來(lái)更改我的代碼。提前致謝。順便說(shuō)一句,我無(wú)法更改 if 語(yǔ)句中的條件,因?yàn)橥ㄟ^(guò)輸入給定的命令不一樣,但像這樣以及更多:追加 15插入它 0 25remove_it 30代碼工作得很好我需要建議,使它成為良好的代碼實(shí)踐,以提高我在 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 回答

?
絕地?zé)o雙

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊

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


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]


查看完整回答
反對(duì) 回復(fù) 2022-12-27
  • 1 回答
  • 0 關(guān)注
  • 88 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

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