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

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

滿足條件后繼續(xù)使用更改的參數(shù)進(jìn)行迭代

滿足條件后繼續(xù)使用更改的參數(shù)進(jìn)行迭代

一只甜甜圈 2023-08-08 16:27:54
我需要更改循環(huán)外的參數(shù)并在滿足特定條件后繼續(xù)迭代。例如,我有披薩列表和字符串列表。我有在滿足條件后應(yīng)該切換的醬汁包列表。如果字符串中存在條件“sauce_error”,我需要從醬包列表中切換到另一個(gè)醬包,并使用下一個(gè)醬包繼續(xù)迭代。#list of sauce packssaucePacks = ['sauce_pack1', 'sauce_pack2', 'sauce_pack3', 'sauce_pack4']index = 0saucePack = saucePacks[index]#list of pizzaspizzas = ['pizza1', 'pizza2', 'pizza3_sauce_error', 'pizza4', 'pizza5']#iterating over stringsfor pizza in pizzas:    if 'sauce_error' in pizza:    index = index+1 #need to switch sauce pack if substring in string    print(saucePack, pizza)  else:    print(saucePack, pizza)當(dāng)“pizza”字符串中出現(xiàn)“sauce_error”時(shí),應(yīng)將醬包切換到列表中的下一個(gè)。期望的輸出:sauce_pack1 pizza1sauce_pack1 pizza2sauce_pack2 pizza3_sauce_errorsauce_pack2 pizza4sauce_pack2 pizza5
查看完整描述

3 回答

?
12345678_0001

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

您只是忘記使用新索引更新您的 saucePack:


#list of sauce packs

saucePacks = ['sauce_pack1', 'sauce_pack2', 'sauce_pack3', 'sauce_pack4']

index = 0

saucePack = saucePacks[index]


#list of pizzas

pizzas = ['pizza1', 'pizza2', 'pizza3_sauce_error', 'pizza4', 'pizza5']


#iterating over strings

for pizza in pizzas:  

    if 'sauce_error' in pizza:

        index = index+1 #need to switch sauce pack if substring in string

        saucePack = saucePacks[index]

        print(saucePack, pizza)

    else:

        print(saucePack, pizza)


查看完整回答
反對(duì) 回復(fù) 2023-08-08
?
尚方寶劍之說(shuō)

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

使用iteraor會(huì)更Pythonic?,而不是更新索引并根據(jù)它獲取新元素:

#list of sauce packs

saucePacks = iter(['sauce_pack1', 'sauce_pack2', 'sauce_pack3', 'sauce_pack4'])

saucePack = next(saucePacks)


#list of pizzas

pizzas = ['pizza1', 'pizza2', 'pizza3_sauce_error', 'pizza4', 'pizza5']


#iterating over strings

for pizza in pizzas:??

? ? if 'sauce_error' in pizza:

? ? ? ? saucePack = next(saucePacks)? # takes the next sauce from the list

? ? print(saucePack, pizza)

請(qǐng)注意,當(dāng)?shù)骱谋M時(shí),您需要小心(您還需要對(duì)索引進(jìn)行處理,以防它超過(guò)列表的大小......)


查看完整回答
反對(duì) 回復(fù) 2023-08-08
?
慕哥9229398

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

如果該代碼對(duì)您有意義,請(qǐng)使用它。


#list of sauce packs

saucePacks = ['sauce_pack1', 'sauce_pack2', 'sauce_pack3', 'sauce_pack4']

index = 0


#list of pizzas

pizzas = ['pizza1', 'pizza2', 'pizza3_sauce_error', 'pizza4', 'pizza5']


#iterating over strings

for pizza in pizzas:??

? if 'sauce_error' in pizza:

? ? index += 1 #need to switch sauce pack if substring in string

? ? print(saucePacks[index], pizza)

? else:

? ? print(saucePacks[index], pizza)


查看完整回答
反對(duì) 回復(fù) 2023-08-08
  • 3 回答
  • 0 關(guān)注
  • 209 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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