Given N number of sets. The task is to write a program to perform cartesian product of all the sets in a given order.
Input: 1st set: 1 2 2nd set: A 3rd set: x 4th set: 5 6 Output: [['1', 'A', 'x', '5'], ['1', 'A', 'x', '6'], ['2', 'A', 'x', '5'], ['2', 'A', 'x', '6']] Input: 1st set: 1 2 2nd set: A 3rd set: x y z Output: [['1', 'A', 'x'], ['1', 'A', 'y'], ['1', 'A', 'z'], ['2', 'A', 'x'], ['2', 'A', 'y'], ['2', 'A', 'z']]
Approach: The approach is to compute the product of set-1 and set-2 at the beginning and then the resultant of set-1 and set-2 will have a product with set-3 and then the resultant of set-1, set-2, set-3 will have a Cartesian product with set-4 and so on till set-n.
Below is the implementation of the above approach.
[[1, 'A', 'x'], [1, 'A', 'y'], [1, 'A', 'z'], [2, 'A', 'x'], [2, 'A', 'y'], [2, 'A', 'z']]
- Python | Cartesian product of string elements
- Python | Construct Cartesian Product Tuple list
- Python program to count number of vowels using sets in given string
- Program to determine the quadrant of the cartesian plane
- Ways to express a number as product of two different factors
- Program to calculate product of digits of a number
- Check if the product of digit sum and its reverse equals the number or not
- Sets in Python
- Python Sets
- Python | remove() and discard() in Sets
- Python Set | Pairs of complete strings in two sets
- Python | sympy.sets.Lopen() method
- Python | sympy.sets.open() method
- Python | sympy.sets.Ropen() method
- Python | Number of elements to be removed such that product of adjacent elements is always even
- Python program to find common elements in three lists using sets
- Python | K Value Indices Product
- Product of all the pairs from the given array
- Python | Triple Product to K
- Digit - Product - Sequence
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.