字典TypeError:不可用类型:set

时间:2015-10-13 20:41:43

标签: python list dictionary set typeerror

我正在尝试拆分字符串,然后从该字符串中获取一个关键字并在字典中找到它。然后我想要调出字典的那一部分,但我遇到了这个错误 -

TypeError: unhashable type:set

- 在最后一行:

solutions = {'display': 'take it to a specialist to get fixed','screen':'test'}
problems = ['display','screen','cracked','broken','clear']``
words = ()
query = input("What is the problem? ")
query_split = query.split()
words = query_split
keyword = set(words) & set(problems)
print(keyword)
print (solutions[keyword])

2 个答案:

答案 0 :(得分:2)

当您keyword = set(words) & set(problems)时,关键字变为set。您可能希望使用set函数将关键字设置为pop的元素。

keyword = keyword.pop()

答案 1 :(得分:0)

我不确定任何其他的回答者都在试图了解你在做什么。

试试这个。我不确定你对problems的计划是什么,但这至少会运行并希望能让你朝着正确的方向前进:

solutions = {'display': 'take it to a specialist to get fixed','screen':'test'}
problems = ['display','screen','cracked','broken','clear']
query = raw_input("What is the problem? ")
for word in query.split():
    print(word)
    print(solutions.get(word))

请注意input更改为raw_input并将字典的密钥作为str类型而不是set来访问。