Last post, we reviewed the Dictionary structure which is composed of two pair values. (Key, Value)
This time I am going to make a program which calculate the frequency of each age.
First of all, I would like to reuse the previous program which was posted in title "Simple visualization using (python and R )_part1".
This program read the html tag and finds out the values in accordance with the given pattern.
Because our focused value is just an age, we put all the age values into the list structure called "data2".
Now, I want to use this "data2" list as an argument for the new function called "frequency()".
Let's take look at the source below. I defined a new function with a parameter called "agedata"
We need a dictionary. Read the age until the end of the list and evaluate if given age meets a same age. In this case just increase the value of that age in dictionary structure. This iteration goes over and over again until the end of the list.
def frequency(agedata)::
wordDic = {}
for word in agedata:
if wordDic.has_key(word):
wordDic[word]=wordDic[word]+1
else:
wordDic[word]=1
##################################
## change the dictionary structure to the list structure
##################################
wordDiclist = wordDic.items()
wordDiclist.sort()
for x,y in wordDiclist:
print "result of age and frequency is : %s , %s " %(x,y)
As you might imagine, result will show you the Age : Frequency with sorted by age.
[hadoop10:50:22@MATPLOT]$python agefrequency.py
result of age and frequency is : 19 , 2
result of age and frequency is : 20 , 16
result of age and frequency is : 21 , 20
result of age and frequency is : 22 , 38
result of age and frequency is : 23 , 50
result of age and frequency is : 24 , 49
result of age and frequency is : 25 , 54
result of age and frequency is : 26 , 40
result of age and frequency is : 27 , 39
result of age and frequency is : 28 , 38
result of age and frequency is : 29 , 29
result of age and frequency is : 30 , 19
result of age and frequency is : 31 , 21
result of age and frequency is : 32 , 14
result of age and frequency is : 33 , 23
result of age and frequency is : 34 , 8
result of age and frequency is : 35 , 9
result of age and frequency is : 36 , 6
result of age and frequency is : 37 , 7
result of age and frequency is : 38 , 2
result of age and frequency is : 39 , 2
Next time, I am going to make a program with an another approach but it should be get an same result.
No comments:
Post a Comment