user1882385
user1882385

Reputation: 169

Check if a list item integer is a number between every two item integers in another list in Python?

I have two lists both containing numbers, and I want to check if the numbers from one list are numbers between every two numbers from the other list? The "other" list has ordered numbers so that every two numbers is an interval like 234, 238, 567, 569, 2134, 2156.

ListA = [200, 765, 924, 456, 231]

ListB = [213, 220, 345, 789, 12, 45]

I want to check if ListA[0:] is a number between either ListB[0:1] or [2:3] etc.

I have tried this:

for i in range(0,len(ListB), 2):
    for x in ListA:
        if i < x < (i:i+2):
            print 'within range'

But I get a syntax error for the "i:i+2" :(

Upvotes: 1

Views: 237

Answers (3)

voithos
voithos

Reputation: 70552

If what you want to do is make sure that all items in list A are within at least one of the intervals in list B:

>>> listA = [200, 765, 924, 456, 231]
>>> listB = [213, 220, 345, 789, 12, 45]

>>> intervals = zip(*[listB[i::2] for i in range(2)])

>>> [any(low < x < high for low, high in intervals)
                    for x in listA]
[False, True, False, True, False]

Upvotes: 4

abarnert
abarnert

Reputation: 365657

I think this is what you want, although it's not that clear:

for i in range(0, len(ListB), 2):
    for x in ListA:
        if ListB[i] < x < ListB[i+1]:
            print 'within range'

You're right in your intuition that you can i:i+2 sort of represents a "range", but not quite like that. That syntax can only be used when slicing a list: ListB[i:i+2] returns a smaller list with just 2 elements, [ListB[i], ListB[i+1]], just as you expect, but i:i+2 on its own is illegal. (If you really want to, you can write slice(i, i+2) to mean what you're aiming at, but it's not all that useful here.)

Also, from your description, you want to compare x to the values ListB[i] and ListB[i+1], not just i and i+1 as in your code.

If you wanted to use the slice to generate a range, you could do so, but only indirectly, and it would actually be clumsier and worse in most ways:

for i in range(0, len(ListB), 2):
    brange = range(*ListB[i:i+2])
    for x in ListA:
        if x in brange:
            print 'within range'

But this is checking for a half-open range rather than an open range, and it requires creating the list representing the range, and walking over the whole thing, and it will only work for integers. So, it's probably not what you want. Better to just explicitly use ListB[i] < x < ListB[i+1].

You might want to consider whether there's a more readable way to build a list of pairs of elements from ListB. Without yet knowing about itertools and list comprehensions or generator expressions (I assume), you'd probably have to write something pretty verbose and explicit, but it might be useful practice anyway.

Upvotes: 2

Joran Beasley
Joran Beasley

Reputation: 113940

for i in range(0,len(ListB)-1, 2): #offest 1 less than the length
    for x in ListA:
        if ListB[i] < x < ListB[i+1]: #check if it is between ListB's elements
            print 'within range'

at least assuming I understand what you want ...

Upvotes: 0

Related Questions