Python Script Calculating Average


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Python Script Calculating Average
# 1  
Old 10-19-2014
Python Script Calculating Average

Can anyone explain what each line of the code does and how it works? I have no experience with python so I am not sure how the arrays and such work. I found this code while looking through the forums.

Code:
f = open("exams","r")

l = f.readline()
while l:
    l = l.split(" ")
    values = l[1:]
    
    sum = 0.0
    for v in values:
        sum += float(v)
    
    print "%s %f" % (l[0] , sum / len(values))
    
    l = f.readline()
    
f.close()

Thank you for any help.
# 2  
Old 10-19-2014
Code:
f = open("exams","r") # opens the file exams to read

l = f.readline() # reads the first line
while l: # loop while there's a line
    l = l.split(" ") # split the line into an array
    values = l[1:] # makes a copy of array l without the first element
    
    sum = 0.0 # assign a float 0.0 to sum
    for v in values: # loop through the array values
        sum += float(v) # adds to sum the floating point representation of the current (v) for value
    
    print "%s %f" % (l[0] , sum / len(values)) # display results of average
    
    l = f.readline() # load another line from exams
    
f.close() # close the file exams

This User Gave Thanks to Aia For This Post:
# 3  
Old 10-19-2014
Quote:
Originally Posted by Aia
Code:
f = open("exams","r") # opens the file exams to read

l = f.readline() # reads the first line
while l: # loop while there's a line
    l = l.split(" ") # split the line into an array
    values = l[1:] # makes a copy of array l without the first element
    
    sum = 0.0 # assign a float 0.0 to sum
    for v in values: # loop through the array values
        sum += float(v) # adds to sum the floating point representation of the current (v) for value
    
    print "%s %f" % (l[0] , sum / len(values)) # display results of average
    
    l = f.readline() # load another line from exams
    
f.close() # close the file exams

Thanks so much! What is len?
# 4  
Old 10-19-2014
It is a function of python to return the length (the number of items) in an object.
In this case number of values.
# 5  
Old 10-19-2014
Quote:
Originally Posted by Aia
It is a function of python to return the length (the number of items) in an object.
In this case number of values.
Okay, I want to do something similar but with strings and dictionaries. If I have this file:

Code:
25 apple orange
2 carrot peach chicken
50 banana grape

I want to split this up into id and name where id is the number and name is the rest of the string. Then I want to store the entries into a dictionary. I was looking at the dictionary function and was wondering how it would work. I started it but don't know how to put it into a dictionary.

Code:
f = open("food","r")

l = f.readline()
while l:
	l = l.split(" ")
	id, name = l.split(" ")
        dict = {???}

# 6  
Old 10-19-2014
Quote:
Originally Posted by totoro125

Code:
f = open("food","r")

l = f.readline()
dict = {}
while l:
	l = l.split()
	id, value = l[0], l[1:]
        dict[id] = value


Last edited by Aia; 10-19-2014 at 10:12 PM..
This User Gave Thanks to Aia For This Post:
# 7  
Old 10-19-2014
Can I just add dict.sort to sort it numerically by id? And once I sort it, how do I print it out? I didn't really understand the %formatting from the previous one.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculating average from files

I have some files with the following contents.I would like to calculate average of fifth column. How can I do this with awk? file1 cat 95.9 152 78.0 17.9 rat 67.1 153 36.5 30.6 dog 81.4 154 68.1 13.3 dog 92.0 155 55.5 36.5 rat 73.8 156 23.9 49.9 file2 rat... (4 Replies)
Discussion started by: avina
4 Replies

2. UNIX for Dummies Questions & Answers

Calculating average

Hi I have file like below 111,victor,48,12,36 342,Peter,54,58,30 476,Scott,25,36,48 567,Patty,74,17,95 I have written below code to calcualte avereage for every id Victor = 48+12+36/3 #!/bin/ksh /usr/xpg4/bin/awk ' BEGIN {FS=","} {sum=0; n=0;i=3 (1 Reply)
Discussion started by: stew
1 Replies

3. Shell Programming and Scripting

Calculating the average of scores

Hi I have 2 files file1 aac 23 25 aac 87 90 aac 33 67 file2 23 0.9 24 0.8 25 0.4 ........ 67 0.55 ........ I want to get output as (11 Replies)
Discussion started by: anurupa777
11 Replies

4. Shell Programming and Scripting

Calculating average with awk

I need to find the average from a file like: data => BW:123 M:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1 0 1 0 0 1 1 1 1 0 0 1 1 0' data => BW:123 N:30 RTD:0 1... (4 Replies)
Discussion started by: Slagle
4 Replies

5. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

6. Shell Programming and Scripting

Calculating average of 100 different files of same size

Hey guys..... I have many files (lets say 100 or more) of same size, and I want to create a new output file and calculate the average of first row fifth column in all files and print it in first row of output file, then 2nd row fifth col in all 100 files and print it in 2nd row of output... (1 Reply)
Discussion started by: CAch
1 Replies

7. UNIX for Dummies Questions & Answers

Calculating weighted average

Dear all, i have 200 values in a file. How can i calculate a weighted average and output into a new file avg.dat? INPUT: file1.dat 1.3453 2.434 2.345 ..... OUTPUT: avg.dat file1: 1.762 Thanks. Po (3 Replies)
Discussion started by: chen.xiao.po
3 Replies

8. UNIX for Dummies Questions & Answers

Calculating average

Hi, i have 12 float variables in a bash file and i want to calculate the average of them. Can any body help? (6 Replies)
Discussion started by: limadario
6 Replies

9. UNIX for Dummies Questions & Answers

Calculating the Number of Rows and Average

Hi All I like to know how can we calculate the number of rows and the average of the values present in the file. I will not know what will be the rowcount, which will be dynamic in nature of the file. eg. 29 33 48 30 28 (6 Replies)
Discussion started by: pk_eee
6 Replies

10. Shell Programming and Scripting

Calculating the average

This is the cronjob ---------------------- root@a7germ:/home/paxtemp > crontab -l|grep test 57 * * * * /home/paxtemp/test_1.sh 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /home/paxtemp/test.sh root@a7germ:/home/paxtemp > This is the contents of test.sh script... (2 Replies)
Discussion started by: kekanap
2 Replies
Login or Register to Ask a Question