我在一個串列中有兩個單獨的串列。我正在嘗試計算第二個串列中的重復項。我可以從串列 a 到串列 b 執行此操作,結果是:
count_list = [['about', 1], ['almost', 1], ['apple', 1], ['break', 1], ['Singapore', 1], ['cider', 1], ['up', 1], ['day', 1]]
first_proc_count = [['turn', 1], ['there', 1], ['pear', 1], ['up', 1], ['XXXXXX', 0], ['drink', 1], ['near', 1], ['up', 1]]
for word in first_proc_count:
for word2 in count_list:
if word[0] == word2[0]:
word[1] = word[1] word2[1]
word2[1] = 0
print(count_list)
print(first_proc_count)
結果 -[['about', 1], ['almost', 1], ['apple', 1], ['break', 1], ['Singapore', 1], ['cider', 1], ['up', 0], ['day', 1]] -[['turn', 1], ['there', 1], ['pear', 1], ['up', 2],['XXXXXX',0],['喝',1],['近',1],['上',1]]
將單詞“up”添加到第二個串列中,并在第一個串列中將“up”設定為 0。
但是我在第二個串列中做同樣的事情時遇到了問題,即。與一個單一的串列。我知道我應該將回圈加一并查看增量回圈。我希望的結果是:
[['轉', 1], ['那里', 1], ['梨', 1], ['上', 3], ['XXXXXX', 0], ['喝', 1], [ '近', 1], ['上', 0]]
我嘗試了 = 1 的范圍和長度,但我一無所獲。這里的第一個問題。請原諒格式錯誤。謝謝。
uj5u.com熱心網友回復:
目前的問題是您將串列與自身進行了兩次比較。以下代碼應解決此問題:
count_list = [['about', 1], ['almost', 1], ['apple', 1], ['break', 1], ['Singapore', 1], ['cider', 1], ['up', 1], ['day', 1]]
first_proc_count = [['turn', 1], ['there', 1], ['pear', 1], ['up', 1], ['XXXXXX', 0], ['drink', 1], ['near', 1], ['up', 1]]
for i in range(len(first_proc_count)):
for word2 in first_proc_count[i 1:]:
if first_proc_count[i][0] == word2[0]:
first_proc_count[i][1] = first_proc_count[i][1] word2[1]
word2[1] = 0
print(count_list)
print(first_proc_count)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446340.html
上一篇:在python回圈中獲取url
