簡體   English   中英

使用默認鍵和列表中的值從 2 個列表創建字典

[英]Create a Dictionary from 2 Lists with Default Keys and the Values from the Lists

所以我得到了 2 個列表:

letters = ['a', 'b', 'c']    
nums = [1, 2, 3]

預期結果:

dict = {
'Letter': 'A',
'Number': 1,
'Letter' : 'B',
'Number': 2,
'Letter' : 'C',
'Number': 3,
}

我找到了許多簡單地組合字典或用字典創建字典的解決方案,但沒有解決這個問題。

得到這個結果的方法是什么?

這是以與示例中相同的方式獲取密鑰的唯一方法

letters = ['a', 'b', 'c']    
nums = [1, 2, 3]

dict = {}
dict['Number'] = {}
dict['Letter'] = {}

for x in range(len(letters)):
    dict['Letter'][letters[x]] = letters[x]
    dict['Number'][nums[x]] = nums[x]

print(dict)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM