相关疑难解决方法(0)

在Python中将两个列表转换为字典

想象一下,你有:

keys = ['name', 'age', 'food']
values = ['Monty', 42, 'spam']
Run Code Online (Sandbox Code Playgroud)

生成以下字典的最简单方法是什么?

a_dict = {'name' : 'Monty', 'age' : 42, 'food' : 'spam'}
Run Code Online (Sandbox Code Playgroud)

python dictionary list

1101
推荐指数
10
解决办法
72万
查看次数

在Python中将列表转换为字典

假设我a在Python中有一个列表,其条目可以方便地映射到字典.每个偶数元素代表字典的键,以下奇数元素是值

例如,

a = ['hello','world','1','2']
Run Code Online (Sandbox Code Playgroud)

我想把它转换成字典b,在那里

b['hello'] = 'world'
b['1'] = '2'
Run Code Online (Sandbox Code Playgroud)

什么是语法上最干净的方法来实现这一目标?

python dictionary list

177
推荐指数
5
解决办法
45万
查看次数

标签 统计

dictionary ×2

list ×2

python ×2