Skip to content

Instantly share code, notes, and snippets.

@allynt
Last active April 25, 2022 17:15
Show Gist options
  • Save allynt/ae775119c6118ff512c280325b5daf51 to your computer and use it in GitHub Desktop.
Save allynt/ae775119c6118ff512c280325b5daf51 to your computer and use it in GitHub Desktop.
import itertools
# must be zero-based
CATEGORIES_ORDER = {
"SSSI": 0,
"SAC": 1,
"SPA": 2,
"RAMSAR": 3,
"GCR": 4,
"LNR": 5,
"CNTRYPK": 6
}
category_key = lambda area: area["category"]
sorted_grouped_areas = []
tmp_areas = [
(key, list(group))
for key, group in itertools.groupby(sorted(xx, key=category_key), key=category_key)
]
tmp_areas.sort(key=lambda grouped_area: CATEGORIES.get(grouped_area[0], len(CATEGORIES)))
for key, group in tmp_areas:
sorted_grouped_areas += group
#for key, group in itertools.groupby(sorted(processed_data["areas"], key=category_key), key=category_key):
# sorted_areas.insert(CATEGORIES_ORDER.get(key, len(sorted_areas)), list(group))
#sorted_areas = itertools.chain.from_iterable(sorted_areas)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment