Use existing computation functions where you can, like statistics.mean
. If you can't, like when you identify records with lowest/highest values in a data set, write your own loopy function.
You need some analysis you can't do with packaged functions, like statistics.mean
.
Write a function looping over a data set, doing the computations you need. The deets vary, depending on what you want to compute.
An example function finding the name of the goat with the largest After value.
- def find_largest_after(clean_goat_scores):
- largest_after_value = -99999999999
- largest_after_name = ''
- for record in clean_goat_scores:
- goat_name = record['Goat']
- goat_after_value = record['After']
- if goat_after_value > largest_after_value:
- # Remember the new large value.
- largest_after_value = goat_after_value
- # Remember the name for that record.
- largest_after_name = goat_name
- return largest_after_name, largest_after_value
Line 7 compares the After for the current record with the largest so far. For that to work, you need to initialize the largest-value-so-far variable to something very small (line 2). The first time through the loop, the first value of After will be greater than the largest-value-so-far, and it will become the first value.