我想产生倒置的字典在Python倒Python的字典JSON
import json
def readfile():
with open('/home/krishna/Desktop/data/synonyms.json', 'r') as f:
read_data = f.read()
j = json.loads(read_data)
inverted_dict = dict([v,k] for k,v in j.items())
if __name__ == "__main__":
readfile()
别名文本文件有:
{
viral fever: [
"fever",
"viral fever"
],
sickness: [
"acute sickness",
"typhoid"
],..
}
我面对的错误是:
File "/home/krishna/Desktop/Scripts/jsonparse.py", line 7, in readfile
inverted_dict = dict([v,k] for k,v in j.items())
TypeError: unhashable type: 'list'
我的预期输出字典
fever: viral fever
viral fever: viral fever
acute sickness: sickness
typhoid: sickness
而预期的输出是什么? “发烧”是“病毒性发热”还是“发烧”,“病毒性发热”是关键? –
我编辑了我的问题。 –
你不能有一个列表作为字典键。 – Bonifacio2