You can use the product
function from the itertools
module to get the cartesian product of two sets. Here's an example:
main.py136 chars9 lines
Output:
main.py61 chars2 lines
In this example, set1
contains the numbers 1, 2, and 3, and set2
contains the letters 'a' and 'b'. We use the product
function to get the cartesian product of the two sets, which returns an iterator of tuples. We convert this iterator to a list and print the result.
Note that the order of the sets passed to product
does not affect the result. The cartesian product of {1, 2, 3}
and {'a', 'b'}
is the same as the cartesian product of {'a', 'b'}
and {1, 2, 3}
.
gistlibby LogSnag