Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I’m a bit unsure what you are trying to do. But analyzing your posted code, I guess you want to combine every male with every female once, and put tuples of each combination into a final list.</p> <p>If that is the case, then what you are trying to do is creating a <a href="http://en.wikipedia.org/wiki/Cartesian_product" rel="nofollow">Cartesian product</a>. The Python library has a nice function for this: <a href="http://docs.python.org/py3k/library/itertools.html#itertools.product" rel="nofollow"><code>itertools.product</code></a>. You can use it like this:</p> <pre><code>def mating_pairs(males, females): return set(itertools.product(males, females)) </code></pre> <p>If you want to do it the manual way, then you can use two nested for loops to get all combinations. However the way you did it, by utilizing <code>pop</code> won’t work. What you did in your code is that you iterated over all males and females (after copying the parameters) and then you pop the items from the original sets. That way, very quickly both sets <code>males</code> and <code>females</code> will be empty, as you keep popping from them for every possible combination, without considering that you only get all combinations if you keep reusing individual items.</p> <p>You could fix your code like this, without using pop:</p> <pre><code>def mating_pairs(males, females): pairs = set() # We only iterate over the items, so we don’t modify the original sets. for male in males: for female in females: # And instead of adding individual items by once, and zipping them # later, we just directly add tuples to the set. pairs.add((male, female)) return pairs </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload