I have two lists of the same length:
[1,2,3,4] and [a,b,c,d]
I want to create a dictionary where I have {1:a, 2:b, 3:c, 4:d}
What's the best way to do this?
解决方案dict(zip([1,2,3,4], [a,b,c,d]))
If the lists are big you should use itertools.izip.
If you have more keys than values, and you want to fill in values for the extra keys, you can use itertools.izip_longest.
Here, a, b, c, and d are variables -- it will work fine (so long as they are defined), but you probably meant ['a','b','c','d'] if you want them as strings.
zip takes the first item from each iterable and makes a tuple, then the second item from each, etc. etc.
dict can take an iterable of iterables, where each inner iterable has two items -- it then uses the first as the key and the second as the value for each item.