Created
September 11, 2021 20:58
-
-
Save vlad-bezden/b5211914ac7db48ba65de80fb0a13966 to your computer and use it in GitHub Desktop.
Cutting numbers into fixed buckets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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