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.

Georgy
  • 12,464
  • 7
  • 65
  • 73
  • 3
    Can you post what you have tried so far in Python and where in the Python code you are having trouble please – johnashu Sep 21 '17 at 15:07
  • 1
    Welcome to SO! Please take the [tour] and read [MCVE]. Please note that SO is not a code writing service. – Shawn Mehan Sep 21 '17 at 15:12
  • Possible duplicate of [How to input matrix (2D list) in Python?](https://stackoverflow.com/questions/22741030/how-to-input-matrix-2d-list-in-python) – Georgy Jul 12 '19 at 09:33

4 Answers4

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 
'''
Kaushik NP
  • 6,733
  • 9
  • 31
  • 60
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)
Snochacz
  • 685
  • 1
  • 8
  • 23
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
  • Thanks, but why you put 0 in Range? – Himanshu Rastogi Sep 21 '17 at 16:28
  • 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. – Martin Castro Alvarez Sep 21 '17 at 17:00
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)