Skip to Content
Functional Python Programming - Second Edition
book

Functional Python Programming - Second Edition

by Steven F. Lott
April 2018
Intermediate to advanced content levelIntermediate to advanced
408 pages
10h 42m
English
Packt Publishing
Content preview from Functional Python Programming - Second Edition

Building a mapping by sorting

If we want to implement this without using the Counter class, we can use a more functional approach of sorting and grouping. Following is a common algorithm:

from typing import Dict, Any, Iterable, Tuple, List, TypeVarLeg = Tuple[Any, Any, float]T_ = TypeVar("T_")def group_sort1(trip: Iterable[Leg]) -> Dict[int, int]:    def group(            data: Iterable[T_]        ) -> Iterable[Tuple[T_, int]]:        previous, count = None, 0        for d in sorted(data):            if d == previous:                count += 1            elif previous is not None:  # and d != previous                yield previous, count                previous, count = d, 1            elif previous is None:                previous, count = d, 1            else:                raise Exception("Bad bad design problem.")        yield previous, count quantized = (int(5*(dist//5)) for beg, end, dist in ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Functional Python Programming - Third Edition

Functional Python Programming - Third Edition

Steven F. Lott

Publisher Resources

ISBN: 9781788627061Supplemental Content