[英]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.