Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Created September 11, 2021 20:58
Show Gist options
  • Save vlad-bezden/b5211914ac7db48ba65de80fb0a13966 to your computer and use it in GitHub Desktop.
Save vlad-bezden/b5211914ac7db48ba65de80fb0a13966 to your computer and use it in GitHub Desktop.
Cutting numbers into fixed buckets
"""Cutting numbers into fixed buckets
This is a solution for StackOverflow question
https://datascience.stackexchange.com/questions/45118/cutting-numbers-into-fixed-buckets
"""
from bisect import bisect
from random import sample
data = sample(range(10_000), 1_000)
breakpoints = [1, 5, 25, 50, 150, 250, 1_000, 5_000, 10_000]
buckets = {}
for i in data:
buckets.setdefault(breakpoints[bisect(breakpoints, i)], []).append(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment