473,472 Members | 1,800 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Is every number in a list in a range?

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
13 1730
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
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
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
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
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
"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
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
"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
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
"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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: Dag | last post by:
Is there a python module that includes functions for working with prime numbers? I mainly need A function that returns the Nth prime number and that returns how many prime numbers are less than N,...
11
by: lostinpython | last post by:
I'm having trouble writing a program that figures out a prime number. Does anyone have an idea on how to write it? All I know is that n > 2 is prim if no number between 2 and sqrt of n...
8
by: Aaron | last post by:
I need some help writing this function the function returns a list of random non-duplicate integers in a string, 1 parameter indicating the maximum range. string randGen(int maxRange) ...
11
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
3
by: Kriston-Vizi Janos | last post by:
Dear Mr. Kern, and Members, Thank you very much for the fast answer, my question became over-simplified. My source code is appended below. It uses two text files (L.txt and GC.txt) as input...
22
by: joh12005 | last post by:
hello, i'm looking for a way to have a list of number grouped by consecutive interval, after a search, for example : => , , , ]
30
by: Chaos | last post by:
As my first attempt to loop through every pixel of an image, I used for thisY in range(0, thisHeight): for thisX in range(0, thisWidth): #Actions here for Pixel thisX, thisY But it takes...
9
by: jm.suresh | last post by:
I have a list of numbers and I want to build another list with every second element multiplied by -1. input = wanted = I can implement it like this: input = range(3,12) wanted =
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.