June 2019
Beginner to intermediate
770 pages
19h 24m
English
It makes good sense to incorporate mean and standard deviation features directly into a subclass of list. We can extend list like this:
class StatsList(list): def __init__(self, iterable: Optional[Iterable[float]]) -> None: super().__init__(cast(Iterable[Any], iterable)) @property def mean(self) -> float: return sum(self) / len(self) @property def stdev(self) -> float: n = len(self) return math.sqrt( n * sum(x ** 2 for x in self) - sum(self) ** 2 ) / n
With this simple extension to the built-in list class, we can accumulate data and report statistics on the collection of data items.
Note the relative complexity involved in narrowing the type of the list class. The built-in list structure's type, List, is effectively List[Any] ...
Read now
Unlock full access