当键是字符串而值是列表时,如何填充 python 字典?

Man*_*ava 2 python zip dictionary key list

I need to create a dictionary as shown below:

d = {'a': [2, 3, 4], 'b': [3, 4, 6], 'c': [2, 5, 9]}
Run Code Online (Sandbox Code Playgroud)

I have keys in the form of a list

keys = ['a', 'b', 'c'] 
Run Code Online (Sandbox Code Playgroud)

and values are present as separate lists

values = [[2, 3, 2], [3, 4, 5], [4, 6, 9]]. 
Run Code Online (Sandbox Code Playgroud)

I've tried using

d(zip(keys, values))

but it returns a dictionary as

{'a': [2, 3, 2], 'b': [3, 4, 5], 'c': [4, 6, 9]}

Is there any other method or correction?

Sel*_*cuk 6

You should zip your values list before combining it with your keys:

>>> keys = ['a', 'b', 'c']
>>> values = [[2, 3, 2], [3, 4, 5], [4, 6, 9]]
>>> dict(zip(keys, zip(*values)))
{'a': (2, 3, 4), 'b': (3, 4, 6), 'c': (2, 5, 9)}
Run Code Online (Sandbox Code Playgroud)

If the values must be lists (and not tuples), you can do the following:

>>> dict(zip(keys, map(list, zip(*values))))
{'a': [2, 3, 4], 'b': [3, 4, 6], 'c': [2, 5, 9]}
Run Code Online (Sandbox Code Playgroud)