matching objects by a tuple field criterion

Diez B. Roggisch deets at nospam.web.de
Sun Jun 10 13:24:13 CEST 2007


bullockbefriending bard schrieb:
> i have a large collection of python objects, each of which contains an
> integer 6-tuple as part of its data payload. what i need to be able to
> do is select only those objects which meet a simple tuple element
> wildcard matching criterion. e.g. given the following python objects:
> 
>     object A includes tuple (1,2,3,4,5,6)
>     object B includes tuple (1,4,4,4,11,1)
>     object C includes tuple (1,3,9,1,1,1)
> 
> all tuples are unique. for what it's worth, the values in each field
> are independent of the other fields and range from 1 to 14. although
> 'large', my collection is sparse cf. the 14^6 possible tuples.
> 
> i want to search on *one only* tuple field/value. if my search
> criterion is (*,*,*,4,*,*), then i want to find object A and object B.
> if (1,*,*,*,*,*), i want to find objects A, B, and C, etc. i will only
> ever specify an integer match for one tuple field.
> 
> i can think of some naive approaches, but is there an elegant way to
> do this?

Depends on what you find elegant. Are the criteria runtime-specified, 
and is anything other than the * allowerd, e.g. [1,3,8]? IMHO the best 
thing is to create a filter-function that you then use to... filter :)

Like this:

def create_filter_predicate(criteria):
     """
     criteria is an iterable containing either
     an '*' or a list of comma-separated
     integers
     """
     sub_preds = []
     for i, sub_crit in enumerate(criteria):
         if sub_crit == "*":
            continue
         matching_set = set(int(n) for n in sub_crit.split(","))
         sub_preds.append((i, matching_set))
     def predicate(o):
         t = o.my_tuple
         for i, ms in sub_preds:
             if not t[i] in ms:
                return False
         return True
     return predicate

Diez



More information about the Python-list mailing list