Python Forum
is there a single function to confine a number between two others?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is there a single function to confine a number between two others?
#1
is there a single function to confine a number between two others? i know i can do it with min() and max(), but that actually looks ugly. this should use 3 arguments.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
You're asking if one number has a value between two others? I'd do that initially with the operators like

lower <= n <= upper
I'm not sure this is what you meant by "confine"...

If they're all integers, you can also test it from a range, but that won't work for floats.

n in range(lower, upper + 1)
Reply
#3
It's quite simple to mimic built-in range for floats:

def float_range(start, stop, step):
    current = float(start)
    while current < stop:
        yield current
        current += step

print(*float_range(1, 3, 0.25)) -> 1.0 1.25 1.5 1.75 2.0 2.25 2.5 2.75
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
this:
def confine(a,n,b):
    n=max(n,a)
    n=min(n,b)
    return n
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
I've never heard of a single function that does that operation. I don't see anything wrong with your function as a solution. If you were golfing, you could reduce it to

return min(max(n,a),b)
but if your objection to the first is how it reads, I don't think that's an improvement.
Reply
#6
how it reads in code the i want to be readable is my concern. the reduced code does not make it a readable as i want, hence my step-by-step in my confine() function.

i'm fine with using my own function. i have it in my big collection. i was just interested if i missed one in the Python library. but now i'm curious if someone can come up with a better name.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
There's a Stackoverflow question on it, and other than a numpy solution (which is np.clip()), none of them appear to be as readable as your attempt to me.

I wasn't aware of it, but looks like a more common name for that function is clamp(), and some libraries might have an implementation, but not any in the standard library.
Reply
#8
i like clamo better.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Checking the number of arguments a function takes Chirumer 3 2,111 Jul-06-2021, 04:56 PM
Last Post: Chirumer
  Why is the function returning None for a * b instead of number? omm 10 4,201 Nov-05-2020, 01:17 PM
Last Post: omm
  RuntimeError: Optimal parameters not found: Number of calls to function has reached m bntayfur 0 6,073 Aug-05-2020, 04:41 PM
Last Post: bntayfur
  basic random number generator in replace function krug123 2 2,006 Jul-31-2020, 01:02 PM
Last Post: deanhystad
  Counting number of occurrences of a single digit in a list python_newbie09 12 5,362 Aug-12-2019, 01:31 PM
Last Post: perfringo
  My function won't return the sum unless the last paramater is an odd number. Maximuskiller78 1 2,291 Sep-29-2018, 03:11 AM
Last Post: ichabod801
  Write lambda function in pyhhon to coy data from multiple JSON into a single JSON fil anandmn85 2 4,186 Apr-19-2018, 05:56 AM
Last Post: anandmn85
  argument parser: to execute single function, and subsequent functions also raghu 10 5,764 Mar-12-2018, 06:57 AM
Last Post: wavic
  function for a single string number of syllables tebirkes 2 4,445 Nov-02-2016, 03:44 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020