This question already has an answer here:
这个问题在这里已有答案:
- Convert two lists into a dictionary in Python 12 answers
- 在Python 12答案中将两个列表转换为字典
I have two lists of the same length:
我有两个相同长度的列表:
[1,2,3,4]
and [a,b,c,d]
[1,2,3,4]和[a,b,c,d]
I want to create a dictionary where I have {1:a, 2:b, 3:c, 4:d}
我想创建一个字典,我有{1:a,2:b,3:c,4:d}
What's the best way to do this?
最好的方法是什么?
6 个解决方案
#1
89
dict(zip([1,2,3,4], [a,b,c,d]))
If the lists are big you should use itertools.izip
.
如果列表很大,你应该使用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
.
如果键的值多于值,并且要填写额外键的值,则可以使用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.
这里,a,b,c和d是变量 - 它会正常工作(只要它们被定义),但你可能意味着['a','b','c','d']如果你希望他们成为字符串。
zip
takes the first item from each iterable and makes a tuple, then the second item from each, etc. etc.
zip从每个iterable中获取第一个项目并创建一个元组,然后从每个中创建第二个项目,等等。
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.
dict可以采用可迭代的迭代,其中每个内部iterable都有两个项 - 然后使用第一个作为键,第二个作为每个项的值。
#2
9
>>> dict(zip([1, 2, 3, 4], ['a', 'b', 'c', 'd']))
{1: 'a', 2: 'b', 3: 'c', 4: 'd'}
If they are not the same size, zip
will truncate the longer one.
如果它们的大小不同,则zip会截断较长的一个。
#3
3
dict(zip([1,2,3,4], ['a', 'b', 'c', 'd']))
http://docs.python.org/library/functions.html
http://docs.python.org/library/functions.html
#4
3
If there are duplicate keys in the first list that map to different values in the second list, like a 1-to-many relationship, but you need the values to be combined or added or something instead of updating, you can do this:
如果第一个列表中有重复的键映射到第二个列表中的不同值,如1对多关系,但您需要组合或添加值,或者不需要更新,您可以这样做:
i = iter(["a", "a", "b", "c", "b"])
j = iter([1,2,3,4,5])
k = list(zip(i, j))
for (x,y) in k:
if x in d:
d[x] = d[x] + y #or whatever your function needs to be to combine them
else:
d[x] = y
In that example, d == {'a': 3, 'c': 4, 'b': 8}
在该示例中,d == {'a':3,'c':4,'b':8}
#5
1
I don't know about best (simplest? fastest? most readable?), but one way would be:
我不知道最好的(最简单?最快?最可读?),但有一种方法是:
dict(zip([1, 2, 3, 4], [a, b, c, d]))
#6
0
I found myself needing to create a dictionary of three lists (latitude, longitude, and a value), with the following doing the trick:
我发现自己需要创建一个包含三个列表(纬度,经度和值)的字典,以下是诀窍:
> lat = [45.3,56.2,23.4,60.4]
> lon = [134.6,128.7,111.9,75.8]
> val = [3,6,2,5]
> dict(zip(zip(lat,lon),val))
{(56.2, 128.7): 6, (60.4, 75.8): 5, (23.4, 111.9): 2, (45.3, 134.6): 3}
or similar to the above examples:
或类似于上面的例子:
> list1 = [1,2,3,4]
> list2 = [1,2,3,4]
> list3 = ['a','b','c','d']
> dict(zip(zip(list1,list2),list3))
{(3, 3): 'c', (4, 4): 'd', (1, 1): 'a', (2, 2): 'b'}
Note: Dictionaries are "orderless", but if you would like to view it as "sorted", refer to THIS question if you'd like to sort by key, or THIS question if you'd like to sort by value.
注意:词典是“无序的”,但是如果你想将它视为“已排序”,如果你想按键排序,请参考这个问题,如果你想按值排序,请参考这个问题。
#1
89
dict(zip([1,2,3,4], [a,b,c,d]))
If the lists are big you should use itertools.izip
.
如果列表很大,你应该使用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
.
如果键的值多于值,并且要填写额外键的值,则可以使用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.
这里,a,b,c和d是变量 - 它会正常工作(只要它们被定义),但你可能意味着['a','b','c','d']如果你希望他们成为字符串。
zip
takes the first item from each iterable and makes a tuple, then the second item from each, etc. etc.
zip从每个iterable中获取第一个项目并创建一个元组,然后从每个中创建第二个项目,等等。
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.
dict可以采用可迭代的迭代,其中每个内部iterable都有两个项 - 然后使用第一个作为键,第二个作为每个项的值。
#2
9
>>> dict(zip([1, 2, 3, 4], ['a', 'b', 'c', 'd']))
{1: 'a', 2: 'b', 3: 'c', 4: 'd'}
If they are not the same size, zip
will truncate the longer one.
如果它们的大小不同,则zip会截断较长的一个。
#3
3
dict(zip([1,2,3,4], ['a', 'b', 'c', 'd']))
http://docs.python.org/library/functions.html
http://docs.python.org/library/functions.html
#4
3
If there are duplicate keys in the first list that map to different values in the second list, like a 1-to-many relationship, but you need the values to be combined or added or something instead of updating, you can do this:
如果第一个列表中有重复的键映射到第二个列表中的不同值,如1对多关系,但您需要组合或添加值,或者不需要更新,您可以这样做:
i = iter(["a", "a", "b", "c", "b"])
j = iter([1,2,3,4,5])
k = list(zip(i, j))
for (x,y) in k:
if x in d:
d[x] = d[x] + y #or whatever your function needs to be to combine them
else:
d[x] = y
In that example, d == {'a': 3, 'c': 4, 'b': 8}
在该示例中,d == {'a':3,'c':4,'b':8}
#5
1
I don't know about best (simplest? fastest? most readable?), but one way would be:
我不知道最好的(最简单?最快?最可读?),但有一种方法是:
dict(zip([1, 2, 3, 4], [a, b, c, d]))
#6
0
I found myself needing to create a dictionary of three lists (latitude, longitude, and a value), with the following doing the trick:
我发现自己需要创建一个包含三个列表(纬度,经度和值)的字典,以下是诀窍:
> lat = [45.3,56.2,23.4,60.4]
> lon = [134.6,128.7,111.9,75.8]
> val = [3,6,2,5]
> dict(zip(zip(lat,lon),val))
{(56.2, 128.7): 6, (60.4, 75.8): 5, (23.4, 111.9): 2, (45.3, 134.6): 3}
or similar to the above examples:
或类似于上面的例子:
> list1 = [1,2,3,4]
> list2 = [1,2,3,4]
> list3 = ['a','b','c','d']
> dict(zip(zip(list1,list2),list3))
{(3, 3): 'c', (4, 4): 'd', (1, 1): 'a', (2, 2): 'b'}
Note: Dictionaries are "orderless", but if you would like to view it as "sorted", refer to THIS question if you'd like to sort by key, or THIS question if you'd like to sort by value.
注意:词典是“无序的”,但是如果你想将它视为“已排序”,如果你想按键排序,请参考这个问题,如果你想按值排序,请参考这个问题。