简体   繁体   English

使用默认键和列表中的值从 2 个列表创建字典

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

So I got 2 lists:所以我得到了 2 个列表:

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

Desired results:预期结果:

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

I found many solutions for simply combining dicts or creating a dict with dicts but nothing to solve this.我找到了许多简单地组合字典或用字典创建字典的解决方案,但没有解决这个问题。

What would be the way to get this result?得到这个结果的方法是什么?

This is the only way to do it to get the key the same way as in your example这是以与示例中相同的方式获取密钥的唯一方法

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