0

Up to this point I have thought using the += operator was the same as using something such as n = n + 1. In the below code I return different results when replacing the word_count[word] = word_count[word] + 1 with a += expression. What is the difference?

def word_count_dict(filename):
    word_count = {}
    opened_file = open(filename, 'r')
    for lines in opened_file:
        words = lines.split()
    for word in words:
        word = word.lower()
        if not word in word_count:
            word_count[word] = 1
        else:
            word_count[word] = word_count[word] + 1

    opened_file.close()
    return word_count
zerkms
  • 249,484
  • 69
  • 436
  • 539
MARyan87
  • 1,656
  • 2
  • 12
  • 15
  • This same question was asked at around the same time yesterday – Inkblot Dec 16 '15 at 20:26
  • For any expr, `n += x` is equal to `n = n+x` – intboolstring Dec 16 '15 at 20:27
  • Since my question is really based on my misunderstanding its hard to post a sample script. If it clarifies anything this program reads a file and returns the number of appearances for each word. Using += the string 'we' returned '32' and using the same code with word_count[word] = word_count[word] + 1 returned '6' – MARyan87 Dec 16 '15 at 20:33
  • That hardly seems possible. Perhaps when you reduce your script to the barest essentials, you'll discover the actual problem, unrelated to `+=`. – Robᵩ Dec 16 '15 at 20:37
  • 1
    There are some subtle differences between `a += b` and `a = a + b` relating to mutable objects, but none of those differences would trigger here. – user2357112 Dec 16 '15 at 20:38
  • 1
    Possible duplicate of [Why does += behave unexpectedly on lists?](http://stackoverflow.com/questions/2347265/why-does-behave-unexpectedly-on-lists) – ivan_pozdeev Dec 17 '15 at 00:49

1 Answers1

2

I tested:

    word_count[word] = word_count[word] + 1

and:

    word_count[word] += 1

and found both return results to be the same. Have you uploaded the complete code because I've not been able to duplicate your problem, perhaps you forgot about some code that changes the ouput?

Also, instead of open() and close() you should use the "with" statement as it is more secure to use. Example:

def word_count_dict(filename):
     word_count = dict()
     with open(filename, 'r') as file:
         for lines in file:
             words = lines.split()
         for word in words:
             word = word.lower()
             if not word in word_count:
                 word_count[word] = 1
             else:
                 word_count[word] += 1
     return word_count

EDIT:

One can use the defaultdict in collections() to refactor the code

from collections import defaultdict

def word_count_dict(filename):
     word_count = defaultdict(int)
     with open(filename, 'r') as file:
         for lines in file:
             words = lines.split()
         for word in words:
             word_count[word.lower()] += 1
     return word_count