Rarez
Rarez

Reputation: 129

Python Counter doesn't count

import urllib2
f=urllib2.urlopen("http://www.mbnet.com.pl/dl.txt")
list = range(1,50)
counter={}

for lines in f:
    tab_lines=lines.split(" ")
    formated_tab=tab_lines[-1].strip().split(',')
    #print formated_tab
    for i in formated_tab:
        if i in list:
            counter[i]+=1
print counter.items()

My counter doesn't work and I don't know why :(

This is a list of lottery numbers. I would like count how many times drawn each number.

Upvotes: 0

Views: 1743

Answers (4)

lord63. j
lord63. j

Reputation: 4670

Instead of using:

counter[i] = counter.get(i, 0) + 1

You can also try collections.defaultdict:

counter = defaultdict(int)

So you final version should be look like this:

import urllib2
from collections import defaultdict

f=urllib2.urlopen("http://www.mbnet.com.pl/dl.txt")
list = range(1,50)
counter=defaultdict(int)  # use defaultdict here

for lines in f:
    tab_lines=lines.split(" ")
    formated_tab=tab_lines[-1].strip().split(',')
    for i in formated_tab:
        if int(i) in list:
            counter[i] += 1  # don't worry, be happy :)
sumall=sum(counter.values())
for number, value in counter.items():
    print ('Number {} drawn {} times and it is {}% of all').format(number,value,100*value/sumall)

I'll give you an example to show what collections.defaultdict does here:

>>> from collections import defauldict
>>> a = {}
>>> a['notexist']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'notexist'
>>> b = defaultdict(int)
>>> b['notexist']
0

class collections.defaultdict([default_factory[, ...]]) defaultdict is a subclass of the built-in dict class, so don't be scared, but you can do more with it. Once you specified the default_factory variable, when the key is not exist, defaultdict will supply one for you according to the default_factory. Note this magic will only happen when you using dict['key'] or dict.__getitem__(key).

The doucumetaion is here: collections.defaultdict

Upvotes: 0

Rarez
Rarez

Reputation: 129

So much thank you guys !

I finished this code :) !

import urllib2
f=urllib2.urlopen("http://www.mbnet.com.pl/dl.txt")
list = range(1,50)
counter={}

for lines in f:
    tab_lines=lines.split(" ")
    formated_tab=tab_lines[-1].strip().split(',')
    for i in formated_tab:
        if int(i) in list:
                counter[i] = counter.get(i, 0) + 1
sumall=sum(counter.values())
for number, value in counter.items():
    print ('Number {} drawn {} times and it is {}% of all ').format(number,value,100*value/sumall)

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1124998

You are comparing strings with integers. Your if test never matches:

if i in list:

because each i is a string. Your list variable on the other hand, contains integers:

list = range(1,50)

Convert i to an integer to test against other integers:

if int(i) in list:

Some other remarks:

  • list is not a good variable name; you are masking the built-in type.

  • You could just test if i falls in a range by using comparison operators against the start and end values:

    if 1 <= int(i) < 50:
    

    which would be faster as you don't have to scan through the list each time.

  • You cannot assume that the key is already present in counter. You'd have to test first or use counter.get() to return a default. For example:

    counter[i] = counter.get(i, 0) + 1
    
  • To count your values, you could use the standard library collections.Counter() class:

    from collections import Counter
    
    counter = Counter()
    
    for lines in f:
        tab_lines = lines.split()  # note, no argument!
        formatted_tab = map(int, tab_lines[-1].split(','))
        counter.update(i for i in formatted_tab if 0 < i < 50)
    
    print counter.most_common()
    

    In my testing, I didn't see any numbers in that file that where outside the range of 0 to 50 (exclusive), so you can probably get away with just counter.update(formatted_tab).

Upvotes: 2

geckon
geckon

Reputation: 8774

Apart from Martin Pieters' answer there is another problem. You're accessing a dictionary key that doesn't exist in the dictionary. Instead of

counter[i]+=1

you should use something like

counter[i] = counter.get(i, 0) + 1

Upvotes: 0

Related Questions