S-Mason
S-Mason

Reputation: 1

Sorting and taking highest values from a file

I have a file where the lines look like this:

tom, 10, 20, 0, 100, 0, 100, 70, 80, 90, 0, 80, 85

I want to take the number 10 through the second 100, and use the greatest four values. Then take the average.

I have:

        for i in range((my_dictionary[1:6])) :
              for item in inlist:
                  if item > largest:
                      largest = item
                  elif largest > item > second_largest:
                      second_largest = item
                  elif largest > item > second_largest > third_largest:
                      third_largest = item
                  elif largest > item > second_largest > third_largest > fourth_largest:
                      fourth_largest = item
        q = (sum(inlist))/4

This does not work. I am concerned that each line is not considered a list. The error appears on first line as type is unhashable.

Edit: I want to do this without modules. Also, I am creating a list for the line with : name, q1, q2, q3, q4, q5, q6, a1, a2, a3, a4, midterm, final = line.strip().split(',')

Upvotes: 0

Views: 70

Answers (3)

Brandon
Brandon

Reputation: 50

I assume we're starting like this:

my_dictionary = 'tom', 10, 20, 0, 100, 0, 100, 70, 80, 90, 0, 80, 85

And we only want the numbers from 10 to the second 100, so...

aList = my_dictionary[1:7]

Python's built-in sorted will order the numbers from smallest to largest,

aList = sorted(aList)

Since we want the four largest numbers in a list that is ordered from smallest to largest, we want the last four items in the list.

greatest = aList[-4:]

Finally, to get the average, we can simply sum the values in the greatest list and divide by 4 (since we are averaging a total of 4 values).

average = float(sum(greatest) / 4)

float will just let the answer have decimal places. Without float, the average is 57. With float, the average is 57.5

Upvotes: 0

aghast
aghast

Reputation: 15310

Edited: Here's a complete example, in Python 3. Note I added reversed=True on sorted() call. That was a bug earlier.

Also, please note that there are no "modules" here. These are all functions/keywords/capabilities built in to the base python. No imports required!

input = 'tom, 10, 20, 0, 100, 0, 100, 70, 80, 90, 0, 80, 85'
my_dictionary = input.split(', ')
username = my_dictionary.pop(0)
my_dictionary = list(map(int, my_dictionary))


greatest_4values = sorted(my_dictionary[0:6], reverse=True)[0:4]
average = sum(greatest_4values)/4.0


print("Average = ", average)
print("Greatest 4 values = ", greatest_4values)

Upvotes: 1

ShadowRanger
ShadowRanger

Reputation: 155363

No need for complicated conditionals, just use heapq.nlargest or since it's a small number of items anyway, sort the lot of 'em and slice off the largest:

Your code contains insufficient context to figure out where my_dictionary came from, but the general idea would be to slice off the values you care about to make a list to sort, then average those:

import heapq

top4 = sorted(myvalues[1:7], reverse=True)[:4]
# or heapq
top4 = heapq.nlargest(4, myvalues[1:7])

then average them:

avg = sum(top4) / 4.

Upvotes: 2

Related Questions