在Python中,如何将输入默认字典切换为小写

2024-05-13 02:28:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python dict,看起来像:

defaultdict(<type 'int'>, {u'RT': 1, u'be': 1, u'uniforms': 1, u'@ProFootballWkly:': 1, u'in': 1, u'Nike': 1, u'Brooklyn.': 1, u'ET': 1, u"NFL's": 1, u'will': 1, u'a.m.': 1, u'at': 1, u'unveiled': 1, u'Jimmy': 3, u'11': 1, u'new': 1, u'The': 2, u'today': 1})

我用的是:

^{pr2}$

这将输出前4个单词,其中包括单词“the”我正在尝试合并删除Dolch“常用”单词,在此过程发生之前:

filtered_words = [w for w in word_count \
              if not w in stopwords.words('english')]

问题是,我仍然以单词“The”结尾,因为NLTK中的所有(stopwords)都是小写的。我需要一种方法来获取输入的单词_count并将其转换为小写。我尝试在不同的领域添加lower(),例如:

freq_distribution = nltk.FreqDist(word_count.lower())

但一直没有成功,因为我反复得到以下错误:

AttributeError: 'list' object has no attribute 'lower'

Tags: theintypecountbe单词lowerdict
1条回答
网友
1楼 · 发布于 2024-05-13 02:28:36
filtered_words = [w for w in word_count \
          if w.lower() not in stopwords.words('english')]

在检查是否在stopwords列表中之前,这个小写字母w。所以如果w是“The”,那么在检查之前它将被转换为the。因为“the”在列表中,它将被过滤掉。在

相关问题 更多 >