Go Back   AnandTech Forums > Software > Programming

Forums
· Hardware and Technology
· CPUs and Overclocking
· Motherboards
· Video Cards and Graphics
· Memory and Storage
· Power Supplies
· Cases & Cooling
· SFF, Notebooks, Pre-Built/Barebones PCs
· Networking
· Peripherals
· General Hardware
· Highly Technical
· Computer Help
· Home Theater PCs
· Consumer Electronics
· Digital and Video Cameras
· Mobile Devices & Gadgets
· Audio/Video & Home Theater
· Software
· Software for Windows
· All Things Apple
· *nix Software
· Operating Systems
· Programming
· PC Gaming
· Console Gaming
· Distributed Computing
· Security
· Social
· Off Topic
· Politics and News
· Discussion Club
· Love and Relationships
· The Garage
· Health and Fitness
· Merchandise and Shopping
· For Sale/Trade
· Hot Deals with Free Stuff/Contests
· Black Friday 2014
· Forum Issues
· Technical Forum Issues
· Personal Forum Issues
· Suggestion Box
· Moderator Resources
· Moderator Discussions
   

Reply
 
Thread Tools
Old 11-14-2012, 01:14 AM   #1
lemonhead71
Junior Member
 
Join Date: Oct 2012
Posts: 8
Default Python help- lists calculations

Hi, I'm working on question 2 on http://bohr.wlu.ca/cp104/assignments...p?d=1353042000 and I'm just stumped. I made a function that receives inputs from the user and forms the appropriate list as shown in the question:
Code:
def weighted_average():
    list_number=[]
    total_denominator=0
    user_continue='Y'
    while user_continue=='Y' or user_continue=='y':
        m=int(input('Enter the number of the same items: ').strip())
        x=float(input('Enter the price of the item: ').strip())
        user_continue=str(input('Would you enter a different item? Enter "Y" for yes and "N" for no: ' ).strip())
        mx=[m,x]
        list_number.append(mx)
   
    return
But I' having difficulty in actually doing the weighted average part. If the list was [[2,0.36],[3,1.25]] I know the calculation would be (list_number[0][0]*list_number[0][1]+list_number[1][0]*list_number[1][1])/(list_number[0][0]+list_number[1][0]), but since the number of entries in the list is unknown and up to the user I'm not sure how to code for the weighted average. Thank you for your time.
lemonhead71 is offline   Reply With Quote
Old 11-14-2012, 08:43 AM   #2
mv2devnull
Senior Member
 
Join Date: Apr 2010
Posts: 783
Default

Iterate over the list and sum up the values that has to be summed up.
mv2devnull is online now   Reply With Quote
Old 11-14-2012, 10:23 AM   #3
lemonhead71
Junior Member
 
Join Date: Oct 2012
Posts: 8
Default

Quote:
Originally Posted by mv2devnull View Post
Iterate over the list and sum up the values that has to be summed up.
My problem is i don't know exactly how to sum up the values that has to be summed up. I know I should use a for loop for this but I'm not sure how to get the for loop to iterate in such a way that: list_number[0][0]*list_number[0][1]+list_number[1][0]*list_number[1][1]....list_number[n][0]*list_number[n][1], where n is an integer.
lemonhead71 is offline   Reply With Quote
Old 11-14-2012, 01:29 PM   #4
mv2devnull
Senior Member
 
Join Date: Apr 2010
Posts: 783
Default

Surely Python has a loop syntax that steps an integer i from 0 to n.
mv2devnull is online now   Reply With Quote
Old 11-16-2012, 03:44 AM   #5
Cerb
Elite Member
 
Cerb's Avatar
 
Join Date: Aug 2000
Posts: 15,230
Default

Quote:
Originally Posted by mv2devnull View Post
Surely Python has a loop syntax that steps an integer i from 0 to n.
Yes, it does, but not so directly as some languages: you even said it and didn't know it.

It's, "one those things," with Python. 'For' iterates, so to step through a range of integers, you use an iterator that generates those integers, in order.

If the language is going to be slow, it should be able to do fancy stuff, to make that worthwhile.

Code:
# Python 2:
# range returns a list. Big lists can cause problems.
# xrange returns an iterator, so won't chew up so much RAM.
for i in xrange( 0, n+1 ):
    ...

# Python 3:
for i in range( 0, n+1 ):
    ...

# If stuck in Python 3, and needing to generate a list...
# because no matter how many times others say you
# shouldn't do it that way, sometimes it's the best
# way to make what you need with very little code:
my_list = list( range( begin, end[, step ]) )
However, for the OP's problem, the right way to do it is to iterate over an actual list, as given in the problem description.

Quote:
Originally Posted by lemonhead71 View Post
My problem is i don't know exactly how to sum up the values that has to be summed up. I know I should use a for loop for this but I'm not sure how to get the for loop to iterate in such a way that: list_number[0][0]*list_number[0][1]+list_number[1][0]*list_number[1][1]....list_number[n][0]*list_number[n][1], where n is an integer.
Use an over variable and an under variable. Update them each iteration through the loop. After the loop, divide for the result.
__________________
"The computer can't tell you the emotional story. It can give you the exact mathematical design, but what's missing is the eyebrows." - Frank Zappa
Cerb is online now   Reply With Quote
Old 11-16-2012, 07:23 PM   #6
degibson
Golden Member
 
degibson's Avatar
 
Join Date: Mar 2008
Posts: 1,389
Default

Here's an example of the right way to iterate a list in python:
Code:
x = ["Hello ", "World!\n", "Iterate ", "like ", "this."]
for elem in x:
  print elem
I.e., don't use a counter-controlled loop; python has rich support for iterators.
degibson is offline   Reply With Quote
Reply

Tags
lists, python

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

Forum Jump


All times are GMT -5. The time now is 02:51 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.