繁体   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