Why is my counter not updated by "count == count + 1"?
Question:
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.
Answers:
count == count + 1
You’re using the wrong operator.
The double equal sign ==
is a comparison operator. It tests if the two arguments are equal.
The single equal sign =
is an assignment operator.
Use =
instead of ==
.
-
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
...
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.
count == count + 1
You’re using the wrong operator.
The double equal sign ==
is a comparison operator. It tests if the two arguments are equal.
The single equal sign =
is an assignment operator.
Use =
instead of ==
.
-
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
...