我正在尝试拆分字符串,然后从该字符串中获取一个关键字并在字典中找到它。然后我想要调出字典的那一部分,但我遇到了这个错误 -
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])
答案 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
来访问。