1

I need to createa function that takes in a list of tuples then outputs the total highest, lowest, average, and totals in each tupple.

For example: This is what it would take in:

([(99,86,70,7),(100, 96, 65, 10), (50, 40, 29, 11)]) 

I need a function that takes the highest int in each tuple, but only at index [0]. It needs to then average the numbers together at index [1], then find the lowest int at index [2], then just add up the values in the last index of each tupple.

So the output would look like this:

(100, 74, 29, 28) 

This is what I have now. Its completely wrong and stupid and I find tuples very confusing. I'm trying to do this only using while/for loops but I'm just confused with the tuples and lists.

def grades(listt):
    count=0
    while count < len(listt):
        x=();
        for i in range(0, len(listt)):
            x(listt(0[count])) > x(listt(i[count]))
            print x[count]

print grades([(99,86,70,7),(100, 96, 65, 10), (50, 40, 29, 11)]) 
4

2 回答 2

4
def grades(alist):
    highest, average, lowest, sumvalues = alist[0]
    for i in alist[1:]:
        if i[0] > highest: highest = i[0]
        average += i[1]
        if i[2] < lowest: lowest = i[2]
        sumvalues += i[3]
    average = average / len(alist)
    return highest, average, lowest, sumvalues
于 2013-05-01T06:33:25.527 回答
4

对于列表:

grades = [(99,86,70,7),(100, 96, 65, 10), (50, 40, 29, 11)]

你会这样做:

toFindHighest, toFindAverage, toFindLowest, toFindSum = zip(*grades)

highest = max(toFindHighest)
average = sum(toFindAverage) / float(len(toFindAverage))
lowest = min(toFindLowest)
total = sum(toFindSum)
于 2013-05-01T06:40:49.137 回答