How to make a dictionary by using separate arrays of key and value. e.g I have:
a = np.array([10,20,30])
b = np.array([100,200,300])
I want a dictionary like this:
dic = {10:100,20:200,30:300}
How to make a dictionary by using separate arrays of key and value. e.g I have:
a = np.array([10,20,30])
b = np.array([100,200,300])
I want a dictionary like this:
dic = {10:100,20:200,30:300}
dict
can be constructed from a list of tuples, you can construct that list of tuples using zip
:
>>> dict(zip(a,b))
{10: 100, 20: 200, 30: 300}
If you don't want to create the intermediate list (say you have two very big lists) it's better to use an iterator such as itertools.izip
:
>>> from itertools import izip
>>> dict(izip(a,b))
{10: 100, 20: 200, 30: 300}