tags:

views:

220

answers:

3

I have a list. It contains x lists, each with y elements. I want to pair each element with all the other elements, just once, (a,b = b,a)

EDIT: this has been criticized as being too vague.So I'll describe the history. My function produces random equations and using genetic techniques, mutates and crossbreeds them, selecting for fitness. After a number of iterations, it returns a list of 12 objects, sorted by fitness of their 'equation' attribute. Using the 'parallel python' module to run this function 8 times, a list containing 8 lists of 12 objects (each with an equation attribute) each is returned. Now, within each list, the 12 objects have already been cross-bread with each other. I want to cross-breed each object in a list with all the other objects in all the other lists, but not with the objects within it's own list with which it has already been cross-bread. (whew!)

+7  A: 

itertools.product is your friend.

about removing the duplicates, try with a set of sets.

Now it's a little bit clearer what you want:

import itertools

def recombinate(families):
    "families is the list of 8 elements, each one with 12 individuals"
    for fi, fj in itertools.combinations(families, 2):
        for pair in itertools.product(fi, fj):
            yield pair

basically, take all the 2-combinations of families (of those produced in parallel) and for each pair of families, yield all the pairs of elements.

fortran
I'd like you to show me an example, if you would
Peter Stewart
Having an example of what you _exactly_ want first would help. Vague questions => vague answers.
fortran
OK, I'll edit the question.
Peter Stewart
and now I've edited the answer ;-)
fortran
excellent, thanks fortran.
Peter Stewart
you're welcome :-)
fortran
A: 

First of all, please don't refer to this as an "array". You are using a list of lists. In Python, an array is a different type of data structure, provided by the array module.

Also, your application sounds suspiciously like a matrix. If you are really doing matrix manipulations, you should investigate the Numpy package.

At first glance your problem sounded like something that the zip() function would solve or itertools.izip(). You should definitely read through the docs for the itertools module because it has various list manipulations and they will run faster than anything you could write yourself in Python.

Michael Dillon
itertools looks like what I need. I'll check out zip. Thanks
Peter Stewart
+1  A: 

You haven't made it completely clear what you need. It sounds like itertools should have what you need. Perhaps what you wish is an itertools.combinations of the itertools.product of the lists in your big list.

@fortran: you can't have a set of sets. You can have a set of frozensets, but depending on what it really means to have duplicates here, that might not be what is needed.

Mike Graham
good point... I meant one type that its equality was not dependant on the order of its elements, the set is one but definitively mutable.
fortran