4

Like below C++ Code, how could I use Python to input elements in 2d array? Please, help in writing the same program in Python3.

int main()
{
 int s = 3;
 int a[s][s];
 cout<<"Enter 9 Element in Square Matrix";
 for(int i =0;i<s;i++)
 {
  for(int j =0; j<s;j++)
  {
   cin>>a[i][j];
  }
 }
 cout<<"You Entered";
 for(int i =0;i<s;i++)
 {
  for(int j =0; j<s;j++)
  {
   cout<<a[i][j]<<"\t";
  }
 cout<<endl;
 }
return 0;
}
Output:
Enter 9 Elements in Square Matrix
1
2
3
4
5
6
7
8
9
You Entered: 
1 2 3
4 5 6
7 8 9

If there is a mistake in the program, please don't try to correct it. Thank you.

3
  • 3
    Can you post what you have tried so far in Python and where in the Python code you are having trouble please Commented Sep 21, 2017 at 15:07
  • 1
    Welcome to SO! Please take the tour and read minimal reproducible example. Please note that SO is not a code writing service. Commented Sep 21, 2017 at 15:12
  • Possible duplicate of How to input matrix (2D list) in Python? Commented Jul 12, 2019 at 9:33

4 Answers 4

4

Am gonna use list to store the 2D array here. There are many other structures you can use for storing a 2D array, but for basic needs, this will suffice.

n=int(input("Enter N for N x N matrix : "))         #3 here
l=[]                                                #use list for storing 2D array

#get the user input and store it in list (here IN : 1 to 9)
for i in range(n): 
  row_list=[]                                      #temporary list to store the row
  for j in range(n): 
     row_list.append(int(input()))                 #add the input to row list
  l.append(row_list)                               #add the row to the list

print(l)
# [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

#Display the 2D array
for i in range(n):
  for j in range(n):
    print(l[i][j], end=" ")
  print()                                           #new line

'''
1 2 3 
4 5 6 
7 8 9 
'''
Sign up to request clarification or add additional context in comments.

2 Comments

How to Print particular element by calling its index?
Sorry, didn't get you. You asking how to access data in the 2D list? If so, it is as you would do in C. Like here print(l[0][0]) will give 1
1
s = 3
a = [x[:] for x in [[0] * s] * s]

print("Enter 9 Element in Square Matrix")

for i in range(0, s):
    for j in range(0, s):
        a[i][j] = input()

print("You Entered")

for i in range(0, s):
    line = ''
    for j in range(0, s):
        line += a[i][j] + ' '
    print(line)

2 Comments

why you use 2 line?
Because print has built-in newline and I want to print all of them in the same line. Another way around is importing sys and using sys.stdout.write()
1

If you are not familiar with python, you should create a file called, for example, matrix.py and then add the following content:

matrix_size = 3
matrix = []

print("Enter {} Elements in Square Matrix:".format(matrix_size))
for i in range(0, matrix_size):
    row = []
    for j in range(0, matrix_size):
        row.append(input())
    matrix.append(row)

print("You entered:")
for i in range(0, matrix_size):
    print(" ".join(matrix[i]))

After saving the file, you can execute this file this way:

python3 matrix.py

Here is a sample output:

[martin@M7 tmp]$ python3 matrix.py
Enter 3 Elements in Square Matrix:
1
2
3
1
2
3
7
5
4
You entered:
1 2 3
1 2 3
7 5 4

2 Comments

Thanks, but why you put 0 in Range?
range(0, n) returns all the values in the following set: {0, 1, 2, ... n-1}. Arrays (and matrices) start at position 0. As a consequence, iterating in range(0, 9) will give you the values in {0, 1, 2, 3, 4, 5, 6, 7, 8} which account for the 9 position that you want to use in your example.
0

Say you want to create a 3*3 matrix:

Initialize the matrix as follows:

matrix = [x[:] for x in [[0] * 0] * 0]

Then take the matrix elements as input from the user:

    for i in range(0,3):
        row_list = []
        for j in range(0,3):
            row_list.append(int(input()))
        matrix.append(row_list)

Comments

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.