我有兩個列表:標籤和權重(這些排列在一起:weight [i]用於標籤[i])。標籤可以多次出現。所以,我想要做的是將每個標籤的所有權重加在一起,以獲得每個標籤的總重量。當一個項目出現在第一個列表中時,將第二個列表的內容相加在一起
名單看起來是這樣
tags = ['alternative', 'indie', 'jam rock', 'indie', 'alternative', 'punk']
weights = [100, 20, 45, 50, 75, 50]
我試圖讓會是這樣的:
tags = ['alternative', 'indie', 'jam rock', 'punk']
weights =[175, 70, 45, 50]
我已經使用各種循環試過,但我不能弄清楚如何正確得到這個。我一直在使用.remove(i)
,它將擺脫重複的標籤,但這就是我所能做的一切。
任何想法如何做到這一點?
請讓你的問題更清晰,提供投入和預期產出和到目前爲止你已經嘗試了什麼! –
模式:使用計數器或'zip()'遍歷'weight'列表。使用標籤作爲關鍵字和權重列表構建一個字典作爲值。 – dsh
好的,我已經編輯澄清! –