Is every number in a list in a range?

S

Steven W. Orr

I have a list ll of intergers. I want to see if each number in ll is
within the range of 0..maxnum

I can write it but I was wondering if there's a better way to do it?

TIA

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net
 
M

Matimus

I have a list ll of intergers. I want to see if each number in ll is
within the range of 0..maxnum

I can write it but I was wondering if there's a better way to do it?

TIA

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net

I would probably do something using max and min functions like this:

seq = [...] # your list

if min(seq) >= 0 && max(seq) <= MAXNUM:
do stuff
 
B

bearophileHUGS

Steven W. Orr:
I have a list ll of intergers. I want to see if each number in ll is
within the range of 0..maxnum

One possible nice solution (Python V.2.5):

data = [1, 20, 22, 11, 400, 7]
maxnum = 100
print all(0 <= x <= maxnum for x in data)
maxnum = 1000
print all(0 <= x <= maxnum for x in data)

Bye,
bearophile
 
M

MonkeeSage

I have a list ll of intergers. I want to see if each number in ll is
within the range of 0..maxnum

How about:

maxnum = 100
inlist = range(90, 120)
for i in [i for i in inlist if i >= 0 and i <= maxnum]:
# do something with i, it's in the valid range
print i

Regards,
Jordan
 
B

Bruno Desthuilliers

MonkeeSage a écrit :
I have a list ll of intergers. I want to see if each number in ll is
within the range of 0..maxnum


How about:

maxnum = 100
inlist = range(90, 120)
for i in [i for i in inlist if i >= 0 and i <= maxnum]:

if 0 <= i <= maxnum
# do something with i, it's in the valid range
print i

But I guess Steven is able to come up with such a solution by itself !-)
 
M

Matimus

I have a list ll of intergers. I want to see if each number in ll is
within the range of 0..maxnum
I can write it but I was wondering if there's a better way to do it?

--
Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net

I would probably do something using max and min functions like this:

seq = [...] # your list

if min(seq) >= 0 && max(seq) <= MAXNUM:
do stuff

OOps... I've been writing too much C++ lately (or too little python).
That should read:

if min(seq) >= 0 and max(seq) <= MAXNUM:
do_stuff()
 
P

Paul Rubin

Steven W. Orr said:
I have a list ll of intergers. I want to see if each number in ll is
within the range of 0..maxnum

I can write it but I was wondering if there's a better way to do it?

if all(0 <= x <= maxnum for x in ll): ...

This genexp is better than a loop because it bails out immediately
if it finds an out-of-range x.
 
M

MonkeeSage

This genexp is better than a loop because it bails out immediately
if it finds an out-of-range x.

That's true: assuming that input is sequential. But if it's farily
random (e.g., [10, 20, 12, 46, 202, 5, 102]), then you need a loop/
list comp. to check each index.

Regards,
Jordan
 
P

Paul Rubin

MonkeeSage said:
This genexp is better than a loop because it bails out immediately
if it finds an out-of-range x.

That's true: assuming that input is sequential. But if it's farily
random (e.g., [10, 20, 12, 46, 202, 5, 102]), then you need a loop/
list comp. to check each index.


Maybe I didn't undrestand the question. Say maxnum is 30 in your
example above. Then as soon as 46 is seen, you can stop checking, I
thought.
 
M

MonkeeSage

Maybe I didn't undrestand the question. Say maxnum is 30 in your
example above. Then as soon as 46 is seen, you can stop checking, I
thought.

Yes, as long as 29 doesn't follow 46.

inlist = [1, 5, 23, 46, 29, 21]

If you want to preserve the items after 46, then you can't just bail
when you see 46. You need to keep looking at the following values and
comparing them against the boundry conditions. If the the input is
sequential, then this isn't a problem, as all items above 46 are out
of range....but if the input is arbitrary, then you have to look at
each index individually as you know. So it really comes down to if the
data set is ordered or arbitrary as to which approach is more
efficient.

Regards,
Jordan
 
P

Paul Rubin

MonkeeSage said:
If you want to preserve the items after 46, then you can't just bail
when you see 46. You need to keep looking at the following values and
comparing them against the boundry conditions. If the the input is
sequential, then this isn't a problem, as all items above 46 are out
of range....but if the input is arbitrary, then you have to look at
each index individually as you know. So it really comes down to if the
data set is ordered or arbitrary as to which approach is more
efficient.

OK, I didn't read the question that way. I thought the person just
wanted a yes or no answer to whether the list contained any elements
outside the desired range. If the purpose is to select the elements
in the range, then use filter/ifilter or a different listcomp/genexp:

inrange = [x for x in ll if 0<=x<=maxnum]
 
M

MonkeeSage

OK, I didn't read the question that way. I thought the person just
wanted a yes or no answer to whether the list contained any elements
outside the desired range. If the purpose is to select the elements
in the range, then use filter/ifilter or a different listcomp/genexp:

inrange = [x for x in ll if 0<=x<=maxnum]

Well, the OP didn't actually specify what behavior they wanted. I was
just trying to go the with broadest case, with arbitrary values.

Regards,
Jordan
 
D

Dennis Lee Bieber

OK, I didn't read the question that way. I thought the person just
wanted a yes or no answer to whether the list contained any elements
outside the desired range. If the purpose is to select the elements

Which is also my interpretation of the original post:

And while maybe not the most efficient, taking a literal YES/NO for
the entire list...

allInRange = min(ll) >= minimum and max(ll) <= maximum

sure seems to fit that criteria...
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
S

Steven D'Aprano

I have a list ll of intergers. I want to see if each number in ll is
within the range of 0..maxnum

I can write it but I was wondering if there's a better way to do it?

No, the way you wrote it is the absolute best way.

Or possibly the worst way.

It's kinda hard to tell since you don't tell us *how* you do it, so in the
spirit of "there are no stupid questions, only stupid answers" here's my
stupid solution to the problem.


def all_in_range(alist, n1, n2=None):
"""Returns True if all the items in alist of numbers
are in the range n1 through n2 inclusive, using a
silly, obfuscated algorithm.
"""
if n2 is None:
n1, n2 = 0, n1
low, high = n1, n2
all_in = True
i = -1 - len(alist)
if ((i + 1 + len(alist)) < len(alist)) is True:
done = False
else:
done = True
while not done is True:
i = 1 + i
x = alist[i + len(alist)]
p = min(low, x) == low
q = max(high, x) != high
flag = ((not p and not q) or (p and q)) or (not p and q)
all_in = all_in and not flag
if (not (i + len(alist) + 2 > len(alist))) is True:
done = False
else:
done = True
if ((all_in is True) is False) is True:
return False
elif (not (all_in is False)) is True:
return True
 
H

Hendrik van Rooyen

"Steven D'Aprano" <st....ybersource.com.au> wrote:

8< --------------- some code with heavy reliance on Booleans and
'is' -----------

All Right.
This has convinced me.
Some Australians do have a sense of humour.

- Hendrik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top