Need help...

Alex Martelli aleax at aleax.it
Mon May 6 03:51:14 EDT 2002


Shagshag wrote:

> having three keys a, b, c i have to define all possible "couples keys"
        ...
> and i must be able to retrieve (a, b) with (b, a), (a, c) with (c, a)
> and (a, b, c) with (a, c, b), (c, a, b), (b, a, c) or any combination
> of the three...
> i can have couples of 2, 3, 4, or more keys...

So, you want to use arbitrary subsets of (a, b, c, ...) as indices.
The requirement that (a,b) and (b,a) are the same index is obviously
simplest to meet by sorting.


> (a, b) -> v1 -> v2
> (a, c) -> v3 -> v5 -> v8
> (b, c) -> v1 -> v4
> (a, b, c) -> v2 -> v4 -> v7
> 
> where -> stand for something like a linked list.

A Python list is simplest.

> In fact i have more than a thousand possible couples and the linked
> list might be quite long...

You'll need to have enough memory to hold your data, of course, or
else consider using datases or files for support.

If things do fit in memory:

class Shagshag:
    def __init__(self):
        self.store = {}
    def __getitem__(self, indices):
        aux = list(indices)
        aux.sort()
        return self.store.setdefault(tuple(aux),[])

s = Shagshag()

a, b, c = 'abc'
s[b,a,c].append('v2')
s[c,a,b].append('v4')
s[b,c,a].append('v7')

print s[a,b,c]



Alex





More information about the Python-list mailing list