Search code examples
pythonloopscounter

Why is my counter not updated by "count == count + 1"?


 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))

i'm trying to count the number of occurrences of a given word from all_words in each document in a corpus. For some reason, the count is always 0.


Solution

    • You are using a comparison operator (==).

    • What you want instead is an assignment operator (=).

    • To make life a little easier, you can use the increment shortcut of +=.

    • Example:

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