Hi everybody, I just start learning python, and beacuse of that I don't really know all usefull packages and methods, that this language has. But I would like to know how would you write this program, or how would you change it (or may be some part of it) to looks it more elegant and to be more efficient.

here is the code:

import random

class FrequenceCounter:
    count  = 0

    def update_count(self):
        self.count += 1

    def get_count(self):
        return self.count

nums = xrange(10)
freq_of_num = {}
runs = int(raw_input("Enter number of runs "))
for x in xrange(10):
    freq_of_num[x] = FrequenceCounter() 

for x in xrange(runs):
    freq = random.choice(nums)
    freq_of_num[freq].update_count()

for x in xrange(10):
    print "Number %d occurred %d" % (x, freq_of_num[x].get_count())

And that's how my output looks like:

Enter number of runs 88
Number 0 occurred 9
Number 1 occurred 12
Number 2 occurred 10
Number 3 occurred 2
Number 4 occurred 5
Number 5 occurred 13
Number 6 occurred 9
Number 7 occurred 10
Number 8 occurred 11
Number 9 occurred 7

or how would you change it (or may be some part of it) to looks it more elegant and to be more efficient.

here is the code:

You are making 88 random numbers in range 0 to 9 and want to count occurrence.
use randint() because it are bettter suited for this than choice().
In python 2.7--> collections.Counter is nice

>>> from collections import Counter
>>> from random import randint
>>> print Counter([randint(0,9) for i in range(88)])
Counter({7: 12, 1: 11, 3: 10, 8: 10, 9: 10, 0: 9, 2: 7, 4: 7, 5: 7, 6: 5})

So you see it dos the same,or maybe if i do this you see it clearer.

from collections import Counter
from random import randint

runs = 88
numbers = Counter(randint(0,9) for i in range(runs))
for numb in numbers:
    print 'Number {0} occurred {1}'.format(numb, numbers[numb])

'''Output-->
Number 0 occurred 4
Number 1 occurred 7
Number 2 occurred 13
Number 3 occurred 10
Number 4 occurred 5
Number 5 occurred 10
Number 6 occurred 10
Number 7 occurred 11
Number 8 occurred 10
Number 9 occurred 8
'''

Before python 2.7 and still is using dictionary a common way to count stuff.

>>> d = {}
>>> numbers = [randint(0,9) for i in range(88)]
>>> for v in numbers: d[v] = d.get(v, 0) + 1
>>> print d
{0: 4, 1: 11, 2: 10, 3: 4, 4: 13, 5: 8, 6: 12, 7: 7, 8: 11, 9: 8}

So same output with soution this look like this

from random import randint

d = {}
runs = 88
numbers = [randint(0,9) for i in range(runs)]
for v in numbers:
    d[v] = d.get(v, 0) + 1

for k,v in d.items():
    print 'Number {0} occurred {1}'.format(k, v)

'''Output
Number 0 occurred 5
Number 1 occurred 8
Number 2 occurred 8
Number 3 occurred 11
Number 4 occurred 11
Number 5 occurred 7
Number 6 occurred 10
Number 7 occurred 9
Number 8 occurred 9
Number 9 occurred 10
'''
commented: good help +13

WOW ))) Thank you so much, snippsat, for such a nice and fully explained answer!
Appreciate this!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.