0

我在 Python 的 nltk 库中使用 VADER 情绪分析工具。我想知道是否有人知道如何从词典中删除单词而不必在文本文件中手动删除。使用 lexicon.update(new_words) 可以添加新单词。我们也可以使用类似的东西来删除单词吗?

因此,要添加新词,我们可以使用:

sia.lexicon.update(new_words)

我尝试使用 lexicon.remove 删除单词,但这不起作用。任何意见,将不胜感激。

4

1 回答 1

1

我认为这只是字典lexicon。 如果是这种情况,请检查以下代码:VADER

# you can update your lexicon to add key:value pairs
example_dict = {"a":1, "b":2, "c":3, "d":4}
example_dict.update({"e":5})
print(example_dict) # OUTPUT: {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

# you can pop a key from the dictionary to remove the key:value pair
# .pop returns also the value
example_dict.pop("a")
print(example_dict) # OUTPUT: {'b': 2, 'c': 3, 'd': 4, 'e': 5}

# if you're sure that all the values in another dictionary are present in yours,
# you can map the pop function
dict2 = {"b":2, "d":6}
all(map( example_dict.pop, dict2))
print(example_dict) # {'c': 3, 'e': 5}

# you can also merge two dictionaries
dict3 = {"x":44, "y":42}
new_dict = {**example_dict, **dict3}
print(new_dict) # OUTPUT: {'c': 3, 'e': 5, 'x': 44, 'y': 42}

您可以对字典执行许多操作。如果确实是您的情况,请查看文档

于 2018-06-21T20:58:20.103 回答