【问题标题】:How to update counter?如何更新计数器?
【发布时间】:2023-04-11 02:27:01
【问题描述】:
 answers = []

   for syn in all_words :
      count = 0
      result = [syn, count]
      for Document in Corpus:
         for Word in Document:
            if syn == Word :
               count == count + 1
               
      answers.append(tuple(result))

我正在尝试计算语料库中每个文档中 all_words 中给定单词的出现次数。由于某种原因,计数始终为 0。

【问题讨论】:

  • 错字:count == count + 1 应该只是 1 =

标签: python loops counter


【解决方案1】:
  • 您正在使用比较运算符 (==)。

  • 您想要的是赋值运算符 (=)。

  • 为了让生活更轻松,您可以使用增量快捷方式+=

  • 例子:

...
if syn == Word :
  count = count + 1
...

# Is Equivalent To:
...
if syn == Word :
  count += 1
...

【讨论】:

  • 非常感谢!
【解决方案2】:
count == count + 1

您使用了错误的运算符。

双等号== 是一个比较 运算符。它测试两个参数是否相等。

单个等号= 是一个赋值运算符。

使用= 而不是==

【讨论】:

  • 非常感谢!
猜你喜欢
  • 2020-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-13
  • 2023-03-04
相关资源
最近更新 更多