Welcome, guest | Sign In | My Account | Store | Cart

Determination of relatively frequency distribution

Python, 25 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Created by Doni Ganchev, ganchevdoni@gmail.com

s=[0,0,0,1,2,2,3,4]
y0=0
y1=0
y2=0
y3=0
y4=0

for x in s:
  if x==0:
     y0=y0+1
  if x==1:
     y1=y1+1
  if x==2:
     y2=y2+1
  if x==3:
     y3=y3+1
  if x==4:
     y4=y4+1

print y0
print y1
print y2
print y4
   

2 comments

Eric-Olivier LE BIGOT 14 years, 6 months ago  # | flag

-1: It would be much cleaner, general, and concise to hold the results in a dictionary, and to remove all the tests:

counts = dict((v, 0) for v in set(s))
for element in s:
    counts[element] += 1
print counts
Matteo Dell'Amico 14 years, 6 months ago  # | flag

You probably want to have a look at collections.Counter.

Created by Doni on Thu, 1 Oct 2009 (MIT)
Python recipes (4591)
Doni's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks