【发布时间】:2018-08-30 03:16:50
【问题描述】:
您好,我正在尝试将两个列表作为键和值放入字典中,但出现错误
TypeError Traceback (most recent call last)
<ipython-input-535-a88b451e7100> in <module>()
1 #
2 DN = {key: value for key, value in zip(NiW, NiV)}
----> 3 DY = {key: value for key, value in zip(YiW, YiV)}
4 D = dict(DN, **DY)
TypeError: unhashable type: 'list'
我做了一些研究,似乎是嵌套列表的外部列表导致了这个错误,但我不确定
数据
YiW
[['africa', 'trip'],
['asia', 'travel'],
['europe', 'holiday']]
YiV
[[array([-0.34219775, 0.61445 , 0.19807251],
array([ 1.8527551 , 2.4294894 , 0.3062766],
[array([-0.34219775, 0.61445 , 0.19807251, 0.15776388],
array([ 1.8527551 , 2.4294894 , 0.3062766],
[array([-0.34219775, 0.61445 , 0.19807251, 0.15776388],
array([ 1.8527551 , 2.4294894 , 0.3062766]]
创意输出:
{'africa':array([-0.34219775, 0.61445 , 0.19807251],
'trip':array([-0.34219775, 0.61445 , 0.19807251, 0.15776388]}etc..
我尝试了很多方法来删除外部列表: flatten-list-of-lists
concatenate-item-in-list-to-strings
how-to-convert-nested-list-into-dictionary-in-python-where-lst00-is-the-key 但是他们在这种情况下不工作,有人可以帮忙吗?提前谢谢你!
【问题讨论】:
标签: python dictionary