Python help- lists calculations

lemonhead71

Junior Member
Oct 31, 2012
8
0
0
Hi, I'm working on question 2 on http://bohr.wlu.ca/cp104/assignments/asgn16.php?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

Junior Member
Oct 31, 2012
8
0
0
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.
 

Cerb

Elite Member
Aug 26, 2000
17,484
33
86
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. :p

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.

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.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
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.