Python Cartesian Product
Python Cartesian Product, “Cartesian product” refers to generating all possible combinations of elements from a set.
From a mathematical perspective, the product of two sets {1, 2, 3, …, 13} x {C, D, H, S} is a set of 2-tuples of length 52.
{(1, C), (1, D), (1, H), (1, S),
(2, C), (2, D), (2, H), (2, S),
...,
(13, C), (13, D), (13, H), (13, S)}
Run the following command to get the solution:
>>> list(product(range(1, 14), '♣♦♥♠'))
[(1, '♣'), (1, '♦'), (1, '♥'), (1, '♠'),
(2, '♣'), (2, '♦'), (2, '♥'), (2, '♠'),
...
(13, '♣'), (13, '♦'), (13, '♥'), (13, '♠')]
You can perform product operations on any number of iterable sets. The more sets involved in the calculation, the larger the result set.