下面是給出的樣本串列
words = ["PoGo","Spange","lie-Fi"]
定義 = ["Pokemon Go的俚語","收集零錢,無論是從沙發、街上的路人或任何其他眾多方式和方法"。 "當你的手機或平板電腦顯示你已連接到無線網路,但你仍然無法用你的設備加載網頁或使用任何互聯網服務"]
uj5u.com熱心網友回復:
嘗試一下:
cooldictionary = {a : b for (a,b) in zip( words, definitions)}。
通過感謝@Danimesejo,你可以這樣嘗試:
cooldictionary = dict(zip(words, definitions))
uj5u.com熱心網友回復:
下面是實作的代碼。
words = ["PoGo","Spange","lie-fi"]
定義 = ["Pokemon Go的俚語","收集零錢,無論是從沙發、街上的路人或任何其他眾多方式和方法"。 "當你的手機或平板電腦顯示你已連接到無線網路,但你仍然無法用你的設備加載網頁或使用任何互聯網服務"]
cooldictionary = {}
for word in words:
for definition in definitions:
cooldictionary[word] = definition
定義.洗掉(definition)
break
print(cooldictionary)
解釋:
在上面的代碼中,我們創建了一個dictionary,名為cooldictionary,它在word dictionary和definition dictionary中搜索每個元素,并以key和value pair的形式逐一匹配每個索引,并在每次迭代中洗掉它們,直到元素結束,然后它打破回圈,最后列印它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/311638.html
標籤:
