Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Newbee needs Help ref Using Function Statements not Loops(For)

Reply
Thread Tools

Newbee needs Help ref Using Function Statements not Loops(For)

 
 
jstreet10
Guest
Posts: n/a
 
      12-03-2003
How would i use function statements instead of loops(for) in this code? i
have tried some, but not working.

# this program is suppose to read fm a file, computes the average and
reads fm a file
# file grade info: dave, 75; david, 70; davy, 60; john, 70; jon, 60; nina,
55; nino, 50; ray, 65; sherry, 90; terry, 85

import os, sys, string
student_table = {}
grade_counter = 0
input_file = open("grades.txt","r")
sum = 0
for record in input_file.readlines():
grade_counter += 1
record_key , record_value = string.split(record,",")
student_table[record_key] = record_value
sum += int (record_value)
average = sum/grade_counter

input_file.close()
student_names = student_table.keys()
student_names.sort()
output_file = open("processed.txt", "w")

for student_name in student_names:
output_file.write(student_name +"," + student_table
[student_name])
output_file.write("Average grade is" + str(average))
output_file.close()




 
Reply With Quote
 
 
 
 
Sean Ross
Guest
Posts: n/a
 
      12-03-2003
"jstreet10" <(E-Mail Removed)> wrote in message
newsLozb.41415$Ac3.27964@lakeread01...
> How would i use function statements instead of loops(for) in this code? i
> have tried some, but not working.

[snip]

I'm sure someone else will ask you why you want to do it this way,
since your code was already very clean and understandable, so I won't
bother - I'll just show you one way that it can be done without for loops,
and without list comprehensions (which use "for"). Someone else will
probably show you the list comprehension version.

# requires python 2.3+
import string
input_file = open("grades.txt","r")
records = input_file.readlines()
input_file.close()
commasplit = lambda record: string.split(record,",")
record_items = map(commasplit, records)
student_table = dict(record_items)
grade_counter = len(records)
# sum is a builtin in python 2.3
total = sum(map(int, student_table.values()))
average = total/grade_counter

student_names = student_table.keys()
student_names.sort()
output_file = open("processed.txt", "w")
rebuild_info = lambda student_name: \
student_name +"," + student_table[student_name]
student_info = map(rebuild_info, student_names)
map(output_file.write, student_info)
output_file.write("Average grade is " + str(average))
output_file.close()


Perhaps I misunderstood what you were asking for with
"function statements". Perhaps you wanted to define some
functions of your own? Or maybe you were looking for
some builtin functions that did this sort of processing?
I'm not sure.

Anyway, HTH,
Sean

p.s. Small note on your original code:

> for record in input_file.readlines():


in later versions of python this can be written as

for record in input_file:



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Giving an rvalue ref to a function taking an rvalue ref Juha Nieminen C++ 13 08-29-2012 09:25 PM
passing ref to ptr again as ref to ptr.... osama178@gmail.com C++ 22 04-30-2008 10:42 AM
passing ref to ptr again as ref to ptr.... osama178@gmail.com C++ 6 04-29-2008 08:09 AM
passing ref to ptr again as ref to ptr.... osama178@gmail.com C++ 0 04-24-2008 08:23 PM
Newbee needs once more again help with passing arrays out of a function Christian Maier C Programming 4 02-18-2007 11:01 AM



Advertisments