from typing import Sequence
from typing import Any


def _get_cont_out_labels(network_structure: Sequence[Sequence]) -> Any:
  """
  Compute the contracted and free labels of `network_structure`.
  Contracted labels are labels appearing more than once,
  free labels are labels appearing exactly once.
  Computed lists are ordered according to int and ASCII ordering 
  for integer and string values, with first entries in each list
  being ordered integer labels followed by ASCII ordered string 
  labels.
  Returns:
    cont_labels, out_labels: The contracted and free labels
      of `network_structure`.
  """
  flat_labels = [l for sublist in network_structure for l in sublist]
  out_labels = [l for l in flat_labels if flat_labels.count(l) == 1]
  int_out_labels = sorted([o for o in out_labels if not isinstance(o, str)
                          ])[::-1]
  # pylint: disable=unnecessary-lambda
  str_out_labels = sorted([o for o in out_labels if isinstance(o, str)],
                          key=lambda x: str(x))

  cont_labels = []
  for l in flat_labels:
    if (flat_labels.count(l) > 1) and (l not in cont_labels):
      cont_labels.append(l)
  int_cont_labels = sorted([o for o in cont_labels if not isinstance(o, str)])
  # pylint: disable=unnecessary-lambda
  str_cont_labels = sorted([o for o in cont_labels if isinstance(o, str)],
                           key=lambda x: str(x))

  return int_cont_labels, str_cont_labels, int_out_labels, str_out_labels