Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
numpy problem
#1
Hi, my task is to enter two matrices in form of numpy array, which are same dimension (nxn) and their elements are less or equal n. Then I need to overlap those matrices, so that I get matrix of all tuples from {1,2,..n}.
So, I managed to enter matrices, and I got tuples of all elements in form of np. array but some tuples appear multiple times. How can I make loop so that tuple only appears once?

n=int(input("Enter n:"))
A=np.array([])
for i in range(n**2):
el=int(input("Enter element:"))
while (el<1) or (el>n):
print("Wrong entrance!")
el=int(input("Enter element:"))
A=np.append(A,el)
A=np.reshape(A,(n,n))
print("A: ")
print(A)

B=np.array([])
for i in range(n**2):
el=int(input("Enter element:"))
while (el<1) or (el>n):
print("Wrong entrance!")
el=int(input("Enter element:"))
B=np.append(B,el)
B=np.reshape(B,(n,n))
print("B: ")
print(B)

p=[]
for element in A.flat:
p.append(element)

w=[]
for element in B.flat:
w.append(element)

q = [p,w]

cart_prod = [(a,b) for a in q[0] for b in q[1]]
cart_prod=np.array(cart_prod)
print(cart_prod)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to solve problem 1181 of the URI with numpy. thiagohps 4 2,646 Mar-22-2021, 04:11 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020