In: Computer Science
Here is the problem I am working on. I need to write a py program that:
Calculates a Cartesian Product Output, A x B of 2 lists. I want to limit each list to 5 numbers. It is kind of working, but I can't help think that there is a simpler way to express this problem. Please advise...
Here is my code:
def CartesianProductOutput(A,B):
lst = []
for x in A:
t = []
t.append(x)
for y in B:
temp = []
for x in t:
temp.append(x)
temp.append(y)
lst.append(temp)
return lst
A = [1,2,3,4,5]
B = [1,2,3,4,5]
print(CartesianProductOutput(A,B))
HELLO!!
EXPLANATION:
CODE:
#importing product function from itertools module
from itertools import product
a=[1,2,3,4,5]
b=[1,2,3,4,5]
x=[] #an empty list
product(a,b) #using product function
for each in product(a,b): #loop to append to the list
x.append(each) #appending to the empty list x
print(x) #printing the list
OUTPUT: