子列表到字典

use*_*542 3 python dictionary list sublist

所以我有:

a = [["Hello", "Bye"], ["Morning", "Night"], ["Cat", "Dog"]]
Run Code Online (Sandbox Code Playgroud)

我想把它转换成字典.

我试过用:

i = iter(a)  
b = dict(zip(a[0::2], a[1::2]))
Run Code Online (Sandbox Code Playgroud)

但它给了我一个错误: TypeError: unhashable type: 'list'

Ter*_*ryA 7

只是:

>>> a = [["Hello", "Bye"], ["Morning", "Night"], ["Cat", "Dog"]]
>>> dict(a)
{'Cat': 'Dog', 'Hello': 'Bye', 'Morning': 'Night'}
Run Code Online (Sandbox Code Playgroud)

我喜欢python的简单性

您可以在此处查看构建字典的所有方法:

为了说明,以下示例都返回一个等于的字典{"one": 1, "two": 2, "three": 3}:

>>> a = dict(one=1, two=2, three=3)
>>> b = {'one': 1, 'two': 2, 'three': 3}
>>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
>>> d = dict([('two', 2), ('one', 1), ('three', 3)]) #<-Your case(Key/value pairs)
>>> e = dict({'three': 3, 'one': 1, 'two': 2})
>>> a == b == c == d == e
True
Run Code Online (Sandbox Code Playgroud)