On Jun 10, 8:58 pm, bullockbefriending bard <[EMAIL PROTECTED]>
wrote:
> 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?

You are going to have to tell us how many is in a "large" collection,
how often you will do this query, how much memory you have to spare,
how fast you want the answer back, whether you want the answer as a
generator, or in a list/tuple/set/whatever -- or you are going to have
to do a fair bit of prototyping and benchmarking.

Steven has given you one end of the spectrum: a naive serial scan. Now
I'll give you another end: a naive pre-build all possible answers
method:

[quite untested]
# build 14x6 array of empty sets
answer = [[set() for what in range(14)] for slot in range(6)]
# populate the sucker
for obj in obj_list:
    for slot in range(6):
        answer[slot][obj.data[slot]-1].add(obj)

Later, the answer to 'Which objects have the value 11 in slot 3?' is
answer[3][11-1]

HTH,
John

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to