使用Python的N个列表的特定项的总和

2024-10-02 12:22:52 发布

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

我有一项任务是使用NLTK分析N个文本。每个文本都超过10万字,所以计算机很难处理这么多数据,这就是为什么我决定在子列表中标记后拆分每个文本,如下所示:

chunks = [tokens_words[x:x+1000] for x in range (0,len(tokens_words), 1000)]

可能,它工作得很好。你知道吗

然后我需要计算,例如,每个文本中的名词数量。我是这样做的:

for chunk in chunks:
    for key in tagged.keys():
        for noun_tag in noun_tags:
            if tagged[key] == noun_tag:
                noun += 1
    totalNoun.append(noun)

然后我使用sum()并计算百分比。我也试过totalNoun += noun,但在这两种情况下,我都能得到3500%或2498%的smth。你知道吗

我做错了什么?你知道吗


Tags: 数据keyin文本列表fortag计算机
1条回答
网友
1楼 · 发布于 2024-10-02 12:22:52

假设你有两块,一块有13个名词,一块有30个名词。您的代码将执行以下操作:

noun: 0
totalNoun: []

# processing chunk 1 with 13 nouns...

noun : 13
totalNoun : [13]

# processing chunk 2 with 30 nouns...

noun : 43
totalNoun : [13,43]

据我所知,您没有在每个块之后将noun设置为0。即:

for chunk in chunks:
    ## HERE ##
    noun = 0
    ## HERE ##

    for key in tagged.keys():
        for noun_tag in noun_tags:
            if tagged[key] == noun_tag:
                noun += 1
    totalNoun.append(noun)

for chunk in chunks循环中使用noun = 0应该可以做到这一点。你知道吗

相关问题 更多 >

    热门问题