By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,569 Members | 1,422 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,569 IT Pros & Developers. It's quick & easy.

Is every number in a list in a range?

P: n/a
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
Mar 5 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a
On Mar 5, 11:03 am, "Steven W. Orr" <ste...@syslang.netwrote:
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

Mar 5 '07 #2

P: n/a
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

Mar 5 '07 #3

P: n/a
On Mar 5, 1:03 pm, "Steven W. Orr" <ste...@syslang.netwrote:
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

Mar 5 '07 #4

P: n/a
MonkeeSage a écrit :
On Mar 5, 1:03 pm, "Steven W. Orr" <ste...@syslang.netwrote:
>>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 !-)

Mar 5 '07 #5

P: n/a
On Mar 5, 1:34 pm, "Matimus" <mccre...@gmail.comwrote:
On Mar 5, 11:03 am, "Steven W. Orr" <ste...@syslang.netwrote:
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
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()

Mar 6 '07 #6

P: n/a
"Steven W. Orr" <st****@syslang.netwrites:
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.
Mar 6 '07 #7

P: n/a
On Mar 5, 7:24 pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
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

Mar 6 '07 #8

P: n/a
"MonkeeSage" <Mo********@gmail.comwrites:
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.
Mar 6 '07 #9

P: n/a
On Mar 5, 9:03 pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
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

Mar 6 '07 #10

P: n/a
"MonkeeSage" <Mo********@gmail.comwrites:
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]
Mar 6 '07 #11

P: n/a
On Mar 5, 9:19 pm, Paul Rubin <http://phr...@NOSPAM.invalidwrote:
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

Mar 6 '07 #12

P: n/a
On Mon, 05 Mar 2007 14:03:50 -0500, Steven W. Orr wrote:
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
--
Steven.

Mar 6 '07 #13

P: n/a
"Steven D'Aprano" <st....ybersource.com.auwrote:

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

Mar 7 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.