Need help...

Patrick W quitelikely at yahoo.com.au
Mon May 6 04:04:15 EDT 2002


shagshag13 at yahoo.fr (Shagshag) writes:

> Hello
> 
> (sorry for my poor english)
> 
> I need to build some kind of python object(s) which could handle these
> :
> 
> having three keys a, b, c i have to define all possible "couples keys"
> 
> (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.
> 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...

Hmmm. Not quite sure what you're looking for, but it seems that at
least *part* of the solution requires a 'permutations' function, which
might look something like:

def permutations(ls):
    def remove_one(alist, item):
        result = alist[:]
        result.remove(item)
        return result
    if ls == []: return [[]]
    return [[x] + y \
            for x in ls \
            for y in permutations(remove_one(ls, x))]

Does that help?



More information about the Python-list mailing list