【发布时间】:2019-03-21 15:44:19
【问题描述】:
我是 Python 的初学者,我有一个字典,如下所示:
thisdict ={ "Animal", "Metal", "Car"}
我得到他们的同义词如下:
syns = {w : [] for w in thisdict}
for k, v in syns.items():
for synset in wordnet.synsets(k):
for lemma in synset.lemmas():
v.append(lemma.name())
print(syns)
目前,Animal 的 syns 输出为:
{'Animal': ['animal']}
{'Animal': ['animal', 'animate_being']}
{'Animal': ['animal', 'animate_being', 'beast']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal', 'carnal']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal', 'carnal', 'fleshly']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal', 'carnal', 'fleshly', 'sensual']}
我的问题是,有没有办法创建一个字典,其中每行包含一个单词并且它是同义词,例如:
Animal: 'animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal', 'carnal', 'fleshly', 'sensual'
Cat: ...
编辑 感谢 DannyMoshe,我在追加之前添加了 if not key.lower() == lemma.name(): 现在有以下输出:
['animate_being']
['animate_being', 'beast']
['animate_being', 'beast', 'brute']
['animate_being', 'beast', 'brute', 'creature']
['animate_being', 'beast', 'brute', 'creature', 'fauna']
['animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal']
['animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal', 'fleshly']
['animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal', 'fleshly', 'sensual']
有没有办法选择最后一行,['animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal', 'fleshly', 'sensual'],并匹配它在这个dict里给Animal?
【问题讨论】:
-
这不是你已经创建的吗?还是我误解了你想要达到的目标。
-
我试图只列出同义词,所以 animate_being、beast 等而不是 {'Animal': ['animal', 'animate_being', 'beast', 'brute' ,“生物”,“动物”,“动物”,“肉体”,“肉体”,“感性”]}。这个想法是我想创建一个字典(甚至是一个数据框),其中一列有一个单词列表,如果有意义的话,下一列有每个单词的同义词按行水平显示
-
那么在您提供的示例中,您想从列表中删除动物?
-
是的,并且只保留最后一行包含我想在 thisdict 中与 Animal 匹配的所有同义词。
-
所以在追加之前,只需添加一个 if 语句:
if not k.lower() == lema.name(): v.append(lemma.name())。我认为这应该会给你想要的输出..
标签: python-3.x dictionary wordnet