Python exercise with url and string counting
Question: i'm having a little problem with an exercise i have to do : Basically the
assignment is to open an url, convert it into a given format, and count the
number of occurrences of given strings in the text.

    
    
    import urllib2 as ul 
    
    def word_counting(url, code, words):
        page = ul.urlopen(url)
        text = page.read()
        decoded = ext.decode(code)
        result = {}
    
        for word in words:
            count = decoded.count(word)
            counted = str(word) + ":" + " " + str(count)
            result.append(counted)
    
        return finale
    

The result i should get is like " word1: x, word2: y, word3: z " with x,y,z
being the number of occurrences. But it seems that i only get ONE number, when
i try to run the test program i get as result only like 9 for the first
occurrences, 14 for the second list, 5 for the third, missing the other
occurrences and the whole counted value. What am i doing wrong? Thanks in
advance


Answer: You're not appending to the dictionary correctly.

The correct way is `result[key] = value`.

So for your loop it would be

    
    
    for word in words:
      count = decoded.count(word)
      result[word] = str(count)
    

An example without decode but using `.count()`

    
    
    words = ['apple', 'apple', 'pear', 'banana']
    result= {}
      for word in words:
        count = words.count(word)
        result[word] = count
    
    >>> result
    >>> {'pear': 1, 'apple': 2, 'banana': 1}     
    

