# # Produced by: # Graham Thompson # captainhammy@gmail.com # www.captainhammy.com # # Name: listutils.py # # Comments: Perform mathematical and statistical operations on lists. # # Version: 1.0 # import random def shuffle(values): """Randomly shuffle the list.""" return random.shuffle(values) def square(values): """ Return the square of each value in the list. """ return [element**2 for element in values] def sumSquares(values): """ Return the sum of the squares of all values. """ return sum(square(values)) def average(values): """ Return the average of the values in the list. """ return sum(values) / float(len(values)) def median(values): """ Return the median value of a list. If there is an even number of elements, it is the higher of the two possible values. """ values.sort() return values[len(values) / 2.0] def rootMeanSquare(values): """ Return the square root of the average of the squares of all values in the list. """ squares = square(values) avg = average(squares) return avg ** 0.5 def standardDeviation(values): """ Computer the standard deviation of the values in the list. """ mean = average(values) differences = [(value - mean)**2 for value in values] return average(differences) ** 0.5