Problem 295065 · easy · Level 02 Linear Data Structures

Mean of Every Column

py-zip · py-comprehensions · dictionaries · mean

A small table of measurements is stored row by row: header names the columns and each row in rows holds one value per column. Write column_means(header, rows) that returns a dictionary mapping each column name to the mean of that column.

Examples

Input:  header = ["height", "weight"]
        rows   = [[170, 65], [180, 80], [160, 59]]
Output: {"height": 170.0, "weight": 68.0}

Input:  header = ["t"], rows = [[1.5], [2.0]]
Output: {"t": 1.75}

Floats are compared with a tolerance of 1e-6.

Constraints

  • 1 <= len(header) <= 100, 1 <= len(rows) <= 10**4
  • Every row has exactly len(header) numbers; column names are distinct.

Goals

  • Turn rows into columns with `zip(*rows)`
  • Pair names with values using `dict(zip(names, values))`
  • Compute a mean per column in one comprehension
Starting Python…