1

I tried to create a matrix using numpy.array with the following code

def matrix_input(3):
    matrix = []
    for i in range(N):
        a = nd.array(input().split(),int)
        matrix.append(a)
    print(matrix)

But I'm getting the following output: [array([1, 1, 1]), array([1, 1]), array([1, 1, 1])]

For the input:

1 1 1 
 1 1 
1 1 1 

I don't want the matrix to have the word array in them... How do I remove it?

1 Answer 1

1

Make it a list on the 4th line of your code. Also, correct your function as mentioned in the code below. Function call and function creation are two different things, so does the arguments you pass into it.

import numpy as np

def matrix_input(N):  # Argument to function while creation is wrong, use N instead of 3.
    matrix = []
    for i in range(N):
        a = list(np.array(input().split(),int))  # Make it a list here
        matrix.append(a)
    print(matrix)

output:

matrix_input(3)
1 1 1
1 1
1 1 1

[[1, 1, 1], [1, 1], [1, 1, 1]]

Alternative method for creating a Proper matrix :

import numpy as np

matrix_1 = np.matrix([[1,1,1],[1,1,0],[1,1,1]])
print(matrix_1)

Output:

[[1 1 1]
 [1 1 0]
 [1 1 1]]
Sign up to request clarification or add additional context in comments.

2 Comments

Is there an official stackoverflow recommendation to use Python-3.x over just Python. Do you have a link for me? I mean Python2 has been deprecated for over a year now. Can't we let it rest in peace?
@user2640045 i don't think there is one. But, i told that to OP because few people may still use python-2. Python-2 has reached end of life that was unknown to me. I don't read that much of dev news. Sorry about that. In my OS however, Python-2 is still a default which is quite weird.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.