要将下面代码的获取结果存储在数据框中
两列一列是实际名称,另一列是新行中的每个同义词
import nltk
from nltk.corpus import wordnet
import pandas as pd
List = ['protest','riot','conflict']
df=[]
def process_genre(str):
for genre in str:
result = []
for syn in wordnet.synsets(genre):
for l in syn.lemmas():
result.append(l.name())
print(set(result))
process_genre(List)
output:
-------
{'resist', 'objection', 'dissent', 'protestation', 'protest'}
{'bacchanalia', 'riot', 'saturnalia', 'belly_laugh', 'scream', 'wow', 'bacchanal', 'thigh-slapper', 'sidesplitter', 'drunken_revelry', 'carouse', 'rioting', 'roister', 'debauchery', 'orgy', 'public_violence', 'howler', 'debauch'}
{'fight', 'battle', 'difference', 'dispute', 'conflict', 'infringe', 'engagement', 'struggle', 'difference_of_opinion', 'contravene', 'run_afoul'}
要将结果存储在数据框中:
# Expected Result:
Col1 Col2
--------------------
protest resist
protest objection
protest dissent
... ...
riot scream
riot carouse
riot saturnalia
... ...
conflict Fight
conflict battle
... ...
这是一个可能的解决方案:
以下是如何使用它:
与编码的逻辑相同,但使用列表理解来构建
pandas
构造函数兼容的结构相关问题 更多 >
编程相关推荐