440,569 Members | 1,422 Online
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
13 Replies

 P: n/a On Mar 5, 11:03 am, "Steven W. Orr" = 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" = 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" >I have a list ll of intergers. I want to see if each number in ll iswithin 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" = 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"

 P: n/a On Mar 5, 7:24 pm, Paul Rubin

 P: n/a "MonkeeSage"

 P: n/a On Mar 5, 9:03 pm, Paul Rubin

 P: n/a "MonkeeSage"

 P: n/a On Mar 5, 9:19 pm, Paul Rubin

 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"

### This discussion thread is closed

Replies have been disabled for this discussion.