-1

I need a program that takes a list of lists and drops the lowest score out of the list and prints the average scores. For example if given: [[100,0,100] , [50,50,0] , [0,0,0]] it should print:

Average for row 0 is 100.0
Average for row 1 is 50.0
Average for row 2 is 0.0

So far I have this but it doesn't allow me to enter a list of lists and it doesn't print to the corresponding rows.

def Averages (listofscores):
    numofrows = 0
    for number in listofscores:
            scores = (sum (listofscores) - min (listofscores)) / ((len (listofscores)) - 1)
            rows = listofscores [0]
            if rows >=0:
                print ('Average for row', numofrows, 'is', scores)
                numofrows = numofrows + 1
            else:
                return None
viraptor
  • 33,322
  • 10
  • 107
  • 191
Pichu2016
  • 1
  • 1
  • 2
  • 6

3 Answers3

3

Loop through, remove the smallest item, then find the average. Very straightforward

scores = [[100,0,100], [50,50,0] , [0,0,0]]

for i, item in enumerate(scores):
    item.remove(min(item))
    print("Average score for row", i, "is", sum(item)/len(item))


Without enumerate (as requested)

i = 0
for item in scores:
    item.remove(min(item))
    print("Average score for row", i, "is", sum(item)/len(item))
    i += 1


One liner just for fun

(Don't actually use this code) If you're curious how it works it gets the index of the smallest item than makes a new list excluding that index using Python's slice notation, then gets the sum of that list and divides it by the length of the original list - 1. It won't be faster because it repeats a lot of the same work.

scores = [[100,0,100], [50,50,0] , [0,0,0]]
print([sum(item[:item.index(min(item))]+item[item.index(min(item))+1:])/(len(item)-1) for item in scores])
#[100.0, 50.0, 0.0]
boardrider
  • 5,882
  • 7
  • 49
  • 86
Keatinge
  • 4,330
  • 6
  • 25
  • 44
  • It's like `for item in scores` except it gives you the index as `i` and the value as `item`. Do you want it without an enumerate? – Keatinge May 18 '16 at 03:43
  • Is there a way to do it without using enumerate? – Pichu2016 May 18 '16 at 03:50
  • Yeah sure you can just keep a counter variable, i'll edit my post to show without enumerate. You should really learn enumerate though, it's super helpful – Keatinge May 18 '16 at 03:50
  • @Pichu2016 I've edited it to show a version that doesn't use enumerate, but I would really suggest you learn how to use it. – Keatinge May 18 '16 at 03:52
  • Thank you I will definitely try. I'm just starting out with Python so tons left to learn. This doesn't really solve my issue of entering lists though. In my program if you try to put more than 1 list in it returns the error that says 2 arguments were entered instead of 1. That's one of the main issues I'm having. – Pichu2016 May 18 '16 at 03:54
  • Can you show example code of what code you are running when you get this error – Keatinge May 18 '16 at 03:55
  • Averages ([100,0,100] , [10,20,30]) Traceback (most recent call last): File "", line 1, in Averages ([100,0,100] , [10,20,30]) TypeError: Averages() takes 1 positional argument but 2 were given – Pichu2016 May 18 '16 at 03:57
  • Well you are still using the old averages function you wrote, thats the problem. My code calculates the averages on those fine. The error is because you are passing two different lists to a function that only accepts one value – Keatinge May 18 '16 at 03:58
  • there are lots of good one liners ... that one however is attrocious :P +1 though – Joran Beasley May 18 '16 at 04:59
  • Yeah that one liner is really disgusting. Is there any better way to do it in one line? I was experimenting with .pop and .remove but those don't return a new list, they just modify the list – Keatinge May 18 '16 at 05:00
  • Although slightly longer, @viraptor's answer has the benefit that it does not modify the data structure when computing the summary statistic. – Neapolitan May 18 '16 at 05:18
1

You're supposed to iterate rows, not specific items. Your function is almost correct:

def Averages (listofrows):
    for rownum, row in enumerate(listofrows):
        score = (sum(row) - min(row)) / (len(row) - 1)
        print('Average for row', rownum, 'is', score)
viraptor
  • 33,322
  • 10
  • 107
  • 191
0

You may read the input fron stdout like this :

l=int(raw_input("Number of Lists"))
lst=[]
while(l>0):
    lst1=raw_input("Enter list : ").split()
    lst.append(lst1)
    l=l-1

print(lst)

Then you may compute the mean/average of each sub-list like this :

for a in lst:
    for elm in a:
        sum=0
        sum =sum + int(elm)
    print("Average for row ",a," is ",sum/(len(a)*1.0))
Ani Menon
  • 27,209
  • 16
  • 105
  • 126