如何使用字典理解来计算文档中每个单词的出现次数

Pap*_*Jha 15 python dictionary list python-2.7 dictionary-comprehension

我有一个充满文本的python列表.就像从每个文档中设置单词一样.因此,对于每个文档,我都有一个列表,然后列出所有文档.

所有列表仅包含唯一的单词.我的目的是计算完整文档中每个单词的出现次数.我可以使用以下代码成功完成此操作:

for x in texts_list:
    for l in x:
        if l in term_appearance:
            term_appearance[l] += 1
        else:
            term_appearance[l] = 1
Run Code Online (Sandbox Code Playgroud)

但是我想用字典理解来做同样的事情.这是我第一次尝试编写字典理解并使用stackoverflow中以前的现有帖子,我已经能够编写以下内容:

from collections import defaultdict
term_appearance = defaultdict(int)

{{term_appearance[l] : term_appearance[l] + 1 if l else term_appearance[l] : 1 for l in x} for x in texts_list}
Run Code Online (Sandbox Code Playgroud)

上一篇文章供参考:

Python中的简单语法错误,否则dict理解

正如上面的帖子所示,我也使用了以下代码:

{{l : term_appearance[l] + 1 if l else 1 for l in x} for x in texts_list}
Run Code Online (Sandbox Code Playgroud)

上面的代码成功生成了空列表,但最终抛出了以下回溯:

[]

[]

[]

[]

Traceback (most recent call last):

  File "term_count_fltr.py", line 28, in <module>

    {{l : term_appearance[l] + 1 if l else 1 for l in x} for x in texts_list}
  File "term_count_fltr.py", line 28, in <setcomp>

    {{l : term_appearance[l] + 1 if l else 1 for l in x} for x in texts_list}

TypeError: unhashable type: 'dict'
Run Code Online (Sandbox Code Playgroud)

任何帮助改善我目前的理解将非常感激.

看着上面的错误,我也试过了

[{l : term_appearance[l] + 1 if l else 1 for l in x} for x in texts_list]
Run Code Online (Sandbox Code Playgroud)

这没有任何错误,但输出只是空列表.

Ana*_*mar 12

就像在其他答案中解释的那样,问题是字典理解会创建一个新字典,因此在创建新字典之前,您不会引用该字典.你无法对你正在做的事情进行字典理解.

鉴于此,您正在尝试重新实现已经完成的工作collections.Counter.你可以简单地使用Counter.示例 -

from collections import Counter
term_appearance = Counter()
for x in texts_list:
    term_appearance.update(x)
Run Code Online (Sandbox Code Playgroud)

演示 -

>>> l = [[1,2,3],[2,3,1],[5,4,2],[1,1,3]]
>>> from collections import Counter
>>> term_appearance = Counter()
>>> for x in l:
...     term_appearance.update(x)
...
>>> term_appearance
Counter({1: 4, 2: 3, 3: 3, 4: 1, 5: 1})
Run Code Online (Sandbox Code Playgroud)

如果你真的想以某种理解方式做到这一点,你可以这样做:

from collections import Counter
term_appearance = Counter()
[term_appearance.update(x) for x in texts_list]
Run Code Online (Sandbox Code Playgroud)

演示 -

>>> l = [[1,2,3],[2,3,1],[5,4,2],[1,1,3]]
>>> from collections import Counter
>>> term_appearance = Counter()
>>> [term_appearance.update(x) for x in l]
[None, None, None, None]
>>> term_appearance
Counter({1: 4, 2: 3, 3: 3, 4: 1, 5: 1})
Run Code Online (Sandbox Code Playgroud)

输出[None, None, None, None]来自列表推导导致该列表(因为这是以交互方式运行),如果您在脚本中运行它python <script>,则该输出将被丢弃.


您还可以使用itertools.chain.from_iterable()从text_lists创建展平列表,然后将其用于Counter.例:

from collections import Counter
from itertools import chain
term_appearance = Counter(chain.from_iterable(texts_list))
Run Code Online (Sandbox Code Playgroud)

演示 -

>>> from collections import Counter
>>> from itertools import chain
>>> term_appearance = Counter(chain.from_iterable(l))
>>> term_appearance
Counter({1: 4, 2: 3, 3: 3, 4: 1, 5: 1})
Run Code Online (Sandbox Code Playgroud)

此外,您的原始代码中的另一个问题 -

{{term_appearance[l] : term_appearance[l] + 1 if l else term_appearance[l] : 1 for l in x} for x in texts_list}
Run Code Online (Sandbox Code Playgroud)

这实际上是嵌套在里面的字典理解的集合理解.

这就是你得到错误的原因 - TypeError: unhashable type: 'dict'.因为在首次运行字典理解和创建之后dict,它正试图将其添加到set.但字典不可清除,因此错误.


Jac*_*hie 6

您获得不可用类型错误的原因是您不能使用字典作为Python中另一个字典的键,因为它们是可变容器.

请参阅:为什么dict对象在python中不可用?