[Tutor] finding numbers in range of of numbers

Richard Lovely roadierich at googlemail.com
Tue Oct 21 14:47:29 CEST 2008


Guessing you mean [5,100] as the inclusive interval notation, so all
but the last element of the example pass?

if any(True for x, y in listoflists if 5 <= x and y <= 100):
   #do stuff

does this do it for you?

Or if you want to know if any elements of the lists within the larger
list are within the range, and the sublists always have two elements
you could do:

if any(True for x,y in listoflists if 5<= x <=100 and 5<= y <=100):
    #do stuff

otherwise, for a list of arbirtary length lists:

from itertools import chain
if any(True for x in chain(*listoflists) if 5<= x <=100):
    #do stuff

You can be slightly faster (but less readable) by changing the
if any(...):
to
if [...]:

This is probably more pythonic, as well.

(There might be a better way, but I like itertools).

Hope something in here was helpful...

On 20/10/2008, Srinivas Iyyer <srini_iyyer_bio at yahoo.com> wrote:
> dear group,
> a simple question that often challenges me.
>
> I have
>
> I have a list of list:
>
> [[10,45],[14,23],[39,73],[92,135]]
>
> I want to identify if any of the items in this list are in range of [5,100]
>
> These numbers are large in original data, I will be using xrange for memory issues.
>
>  thank you.
> srini
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
www.theJNP.com


More information about the Tutor mailing list