Python 字典类型错误:不可损坏类型:集合
我试图拆分一个字符串,然后从该字符串中提取一个关键字并在字典中找到它。然后我想调出字典的那一部分,但我遇到了这个错误-Python 字典类型错误:不可损坏类型:集合,python,list,dictionary,set,typeerror,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 = ()
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])
当您执行
keyword=set(words)&set(problems)
时,关键字将变为集
。您可能希望使用pop
函数将关键字设置为该set
中的元素
i、 e
keyword=keyword.pop()
试试这个。我不确定你对问题的计划是什么,但这至少会让你走上正确的方向:
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\u input
,并以str
类型而不是set
访问字典的键,谢谢-这很有效,甚至可能比其他方法更有效,我只是不确定它是否适合我。