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

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

將列表的元素附加到迭代器的左側(cè)或右側(cè)到另一個列表

將列表的元素附加到迭代器的左側(cè)或右側(cè)到另一個列表

紅顏莎娜 2021-10-26 15:29:00
您可以使用在每次迭代中都會發(fā)生變化的變量;看到這個:->>> list1 = [ 'a', 'b', 'c', 'd' ]>>> >>> var, list2 = 0, []>>> for i in list1:...     filename = 'file{}'.format(var)...     var += 1...     list2.append(filename)... >>> list2['file0', 'file1', 'file2', 'file3']
查看完整描述

3 回答

?
守著一只汪

TA貢獻1872條經(jīng)驗 獲得超4個贊

就個人而言,我認為使用單詞的索引來查找比循環(huán)遍歷列表更容易。


complete_word_list = ['3', 'May', '.', 'Bistritz', '.', 'Left', 'Munich', 'at', '8:35', 'P.', 'M.', ',', 'on', '1st', 'May', ',', 'arriving', 'atVienna', 'early', 'next', 'morning', ';', 'should', 'have', 'arrived', 'at', '6:46', ',', 'but', 'train', 'dracula', 'anhour', 'late']


dracula_list = ['dracula','Dracula']

nearby_words = []


for i in dracula_list:

    if i in complete_word_list: #if word was found in list

        found_word = complete_word_list.index(i) #returns index of word to find

        nearby_words.append(complete_word_list[found_word-1]) #index-1 is the element to the left

        if found_word+1 < len(complete_word_list): #include a check to keep indices in range of list

            nearby_words.append(complete_word_list[found_word+1]) #index+1 is element to the right

print(nearby_words)

編輯:按照建議,您可以使用try and exceptcatch 來檢查元素是否在列表中 ( ValueError) 或者是否有任何相鄰元素 ( IndexError):


complete_word_list = ['3', 'May', '.', 'Bistritz', '.', 'Left', 'Munich', 'at', '8:35', 'P.', 'M.', ',', 'on', '1st', 'May', ',', 'arriving', 'atVienna', 'early', 'next', 'morning', ';', 'should', 'have', 'arrived', 'at', '6:46', ',', 'but', 'train', 'dracula', 'anhour', 'late']

dracula_list = ['dracula','Dracula']

nearby_words = []


for i in dracula_list:

    try:

        found_word = complete_word_list.index(i)

        nearby_words.append(complete_word_list[found_word-1])

        nearby_words.append(complete_word_list[found_word+1])

    except (ValueError, IndexError):

        print('this is either not in the list of there was not an adjacent element on either side.')

print(nearby_words)


查看完整回答
反對 回復(fù) 2021-10-26
?
MM們

TA貢獻1886條經(jīng)驗 獲得超2個贊

使用enumerate,這樣你就可以得到單詞和相應(yīng)的索引:


for i, word in enumerate(complete_word_list):

    if word in dracula_list:

        if i: 

            nearby_words.append(complete_word_list[i-1])

        if i < len(complete_word_list) - 1:

            nearby_words.append(complete_word_list[i+1])


查看完整回答
反對 回復(fù) 2021-10-26
?
qq_花開花謝_0

TA貢獻1835條經(jīng)驗 獲得超7個贊

嘗試這個:


 right= [ complete_word_list[complete_word_list.index(i)+1] for i in dracula_list if i in complete_word_list and complete_word_list.index(i)+1<len(complete_word_list)]


 left= [ complete_word_list[complete_word_list.index(i)-1] for i in dracula_list if i in complete_word_list and complete_word_list.index(i)-1>=0]


 nearby_words = left + right

打印


['train', 'anhour']


查看完整回答
反對 回復(fù) 2021-10-26
  • 3 回答
  • 0 關(guān)注
  • 217 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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