你好,我有一个单词列表,我想检查字典与键和值。其实我只想知道列表中的某些单词是否出现在字典的值中。这在python中可能是一件容易的事情,但我是一名初学者,而且我只是不断收到显然不明白的错误。检查词典中的单词列表
这里是我的代码(字典就在眼前):
words = ["give", "a", "pearl", "to", "the" "elephant"]
for k, v in dic.items():
for word in words:
if word in v:
print(v)
或者:
relevant = {d:reldic[d] for d in reldic if words in reldic[d]}
print(relevant)
错误,我得到:
TypeError: unhashable type: 'list'
缺少什么?
提前致谢!
更新:
好吧,这有助于更好地理解问题。那我的数据看起来像:
2000/9/1 abe D mes Español inan.|m.|m.
2000/9/1 abe D luna Español inan.|m.|m.
2000/9/1 abe D sol Español inan.|m.|m.
2000/9/2 gacuri D meter Español v.t.
2000/9/2 acuri D meter Español v.t.
2000/9/2 yacuri D meter Español v.t.
然后,我有相关的块的集合:
dic = collections.defaultdict(set)
for e in entries:
dic[e[1]].add(e[3])
,最后我的字典里:
reldic = {d:dic[d] for d in dic if len(dic[d]) > 1}
什么是'reldic'? – thefourtheye 2014-10-11 13:20:36
你可能试图用一个列表作为字典键,这是不允许的 – wim 2014-10-11 13:21:08
对不起,不是“reldic”只是“字典”,更新 – 2014-10-11 13:22:05