![]() |
numpy problem - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: numpy problem (/thread-23681.html) |
numpy problem - nats - Jan-12-2020 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) |