我的目录列表是如下:转换列表列表字典
list1 = [ [0, 4, 2, 5, 0, 0],
[4, 0, 0, 0, 6, 4],
[2, 0, 0, 7, 0, 0],
[5, 0, 7, 0, 5, 3],
[0, 6, 0, 5, 0, 1],
[0, 4, 0, 3, 1, 0],
]
,我想转换成字典以下列方式:
G = {'a' : { 'b':4, 'c':2, 'd':5},
'b': {'a':4,'e': 6,'f':4},
'c':{ 'a':2,'d':7},
'd':{'a':5, 'c':7, 'e':5,'f':3},
'e':{'b':6, 'd':5,'f':1},
'f' :{'b':4, 'd':3,'e':1}
}
我有代码下面这样做,但它只是给我一个错误,不可能的类型列表:
list1 = [ [0, 4, 2, 5, 0, 0],
[4, 0, 0, 0, 6, 4],
[2, 0, 0, 7, 0, 0],
[5, 0, 7, 0, 5, 3],
[0, 6, 0, 5, 0, 1],
[0, 4, 0, 3, 1, 0],
]
g=['a','b','c','d', 'e','f']
dl = zip(list1, g)
dict(dl)
print dl
错误是:
File "/Users/zakirtatari/Documents/da.py", line 19, in <module>
dict(dl)
TypeError: unhashable type: 'list'
显示错误。另外,解释你认为你的代码应该如何工作。 –
这是你唯一关心的例子吗?是否有一个更一般的情况,你有超过26行/列,可能会用完字母? – Reti43