Terr_code - 1 year ago 57

Python Question

I want to find the average of all scores above and below the median (not including the median), but I have no idea have to go about doing this.

`import collections`

def main():

names = ["gymnastics_school", "participant_name", "all_around_points_earned"]

Data = collections.namedtuple("Data", names)

data = []

values =[]

with open('state_meet.txt','r') as f:

for line in f:

line = line.strip()

items = line.split(',')

items[2] = float(items[2])

data.append(Data(*items))

values.append(items[2])

print("summary of data:")

sorted_data = sorted (values)

if len(data)%2==0:

a =sorted_data[len(values)//2]

b = sorted_data[len(values)//2-1]

median_val = (a+b)//2

else:

median_val = sorted_data[(len(values)-1)//2]

print(" median score",median_val) #median

Answer Source

We now have `statistics`

as part of the standard library:

```
import statistics
nums = list(range(10))
med = statistics.median(nums)
hi_avg = statistics.mean(i for i in nums if i > med)
lo_avg = statistics.mean(i for i in nums if i < med)
```