def print_avg_by_key(examples, metrics, key_fn):
  """Prints avg results partitioned according to some `key_fn`.

  Args:
    examples: List of Example instances.
    metrics: List of float values corresponding to `examples`.
    key_fn: Function that takes an Example and returns a key to aggregate over.
  """
  key_to_metrics = collections.defaultdict(list)
  for example, metric in zip(examples, metrics):
    key = key_fn(example)
    key_to_metrics[key].append(metric)
  # Compute averages.
  key_to_avg = {
      key: sum(vals) / len(vals) for key, vals in key_to_metrics.items()
  }
  for key, val in key_to_avg.items():
    print("%s (%s): %s" % (key, len(key_to_metrics[key]), val))
Prints avg results partitioned according to some `key_fn`.

  Args:
    examples: List of Example instances.
    metrics: List of float values corresponding to `examples`.
    key_fn: Function that takes an Example and returns a key to aggregate over.
  
language/quest/eval/eval_utils.py