BeeNag
BeeNag

Reputation: 1844

Comparing a list against a tuple

I am trying to compare a list of values against a list of tuples. For example:

list = [1.0, 2.0]

list_of_tuples = [(3, 1.0, 'a'), (4, 2.0, 'b'), (5, 3.0, 'c')]

Ideally what I am trying to do is take the list of tuples and check to see if all the values in that list are present in the other one. In the case of the example lists I provided above 1.0 and 2.0 are present but 3.0 is not. Another problem with this is that the location of the values in the list of tuples is going to be unknown when the comprehension takes place (which isn't the case in the example I provided above) so I'm not actually sure if it is possible to do what I am trying to do.

Any help with this would be appreciated, or an alternative solution would be helpful as well.

Upvotes: 0

Views: 65

Answers (3)

Him
Him

Reputation: 5551

Here's a recursive search algorithm that will return a list of indices at which you can recover the found value (the address reads right to left. There's probably a way to append them in the other direction):

def recursive_search(searchable, value):
    address = []
    if _recursive_search(searchable, value, address):
        return address
    else:
        raise Exception("Value not found!")

def _recursive_search(searchable, value, address):
    if hasattr(searchable, '__iter__'): # check if searchable is a list or a tuple
        for i in range(len(searchable)):
            if _recursive_search(searchable[i], value, address):
                address.append(i)
                return True
        else:
                return False
    else:
        if searchable == value:
            return True

#recursive_search([[[1,2],[3,4],[5,6]],[[7,8],[9,10],[11,12]]], 11) -> [0,2,1]

EDIT If you want to apply that over a list of values, you can use map or some such. map(lambda x: recursive_search(list_of_tuples, x), list)

EDIT If you want to search the OTHER direction as well (which it seems you do in your question), you can combine a recursive map function with recursive_search...

def recursive_map(some_func, thing_to_map_over):
    some_func = some_func or recursive_map
    if hasattr(thing_to_map_over, '__iter__'):
        return map(lambda x: recursive_map(some_func, x), thing_to_map_over)
    else:
        return some_func(thing_to_map_over)

So recursive_map(lambda x: recursive_search(list, x), list_of_tuples) will tell you if everything in list_of_tuples is in list, and where those things are located.

Upvotes: 0

Isdj
Isdj

Reputation: 1856

flt=[]
for tup in list_of_tuples: 
    flt.extend(tup)
for val in lst:
    if val in flt:
        print val
    else:
        print 'failed'

Also, never call a var by a built-in name. call it lst, not list

Upvotes: 0

hspandher
hspandher

Reputation: 16743

If just one value of the tuple must be in the list, then this should give you the list of tuples which are invalid.

filter(lambda values: not any(value in list for value in values), list_of_tuples)

Otherwise, if all values must be in the list

filter(lambda values: not all(value in list for value in values), list_of_tuples)

Upvotes: 1

Related Questions