Better HN
top
new
best
ask
show
jobs
Log in
Sign up
Better HN
Comment
by
parliament32
· 6y ago · 0 comments
Python also doesn't have a built-in clamp function (I think there might be one in numpy), I usually use
sorted((floor, x, ceiling))[1]
Throw it in a function with some asserts if you're worried about x not being a number and giving you weird results.
Comments
[–]
doukdouk
6y ago
There is indeed one in numpy :
https://numpy.org/doc/stable/reference/generated/numpy.clip....
[–]
dagurp
6y ago
You could use statistics.median (in python 3.6+)