In Python how can I write a generic function to generate the Cartesian product of the same set repeated n times without using recursion and without using the itertools package? The function should take two parameters: the set and the n times.
e.g.:
set1={'a','b'}
print({(x,y) for x in set1 for y in set1})
{('a', 'b'), ('b', 'a'), ('b', 'b'), ('a', 'a')}
print({(x,y,z) for x in set1 for y in set1 for z in set1})
{('b', 'a', 'b'), ('a', 'b', 'a'), ('a', 'a', 'a'), ('b', 'a', 'a'), ('a', 'a', 'b'), ('b', 'b', 'a'), ('b', 'b', 'b'), ('a', 'b', 'b')}
etc.
But also:
set2={'a','b','c'}
print({(x,y,z) for x in set2 for y in set2 for z in set2})
print({(w,x,y,z) for w in set2 for x in set2 for y in set2 for z in set2})
etc.