Skip to main content
 首页 » 编程设计

python之为列表中的句子创建单词词典

2025年05月04日14TianFang

我有一个句子列表

a = [['我正在测试'],['我们正在做项目']]

我正在尝试为列表中的所有句子创建一个单词词典。我试过了

vectorizer = CountVectorizer() 
vectorizer.fit_transform(a) 
coffee_dict2 = vectorizer.vocabulary_ 

我收到一个错误 AttributeError: 'list' object has no attribute 'lower'

我期待的结果是一本字典

{'i': 1, 'am': 1, 'testing': 2}

请您参考如下方法:

您需要展平嵌套列表:

from sklearn.feature_extraction.text import CountVectorizer 
coffee_reviews_test = [['i am a testing'],['we are working on project']] 
 
from  itertools import chain 
 
vectorizer = CountVectorizer() 
vectorizer.fit_transform(chain.from_iterable(coffee_reviews_test)) 

另一种解决方案:

vectorizer.fit_transform([x for y in coffee_reviews_test for x in y]) 

coffee_dict2 = vectorizer.vocabulary_ 
print (coffee_dict2) 
{'am': 0, 'testing': 4, 'we': 5, 'are': 1, 'working': 6, 'on': 2, 'project': 3}