I was trying this
n = int(input())
m = int(input())
print(n,m)
for i in range(0,n):
for j in range(0,m):
ar[i][j] = int(input())
for i in range(0,n):
for j in range(0,m):
print (ar[i][j])
But it was showing an error
Traceback (most recent call last):
File "C:/Users/shivansh/Desktop/test.py", line 6, in <module>
ar[i][j] = int(input())
NameError: name 'ar' is not defined
I used to do the same in C language but it works. So, how to this in Python?
You haven't declared ar
yet. In Python, you don't have to perform separate declaration and initialization; nevertheless, you can't perform operations on names willy-nilly.
Start off with something like this:
ar = [[0 for j in range(m)] for i in range(n)]
Since you haven't declared ar before, you cannot assign elements at specific indexes. You can decalre a two dimensional list in this manner initialized to a certain value
.
arr = [[value]*c for _ in range(r)]
where r
and c
are desired number of rows and columns respectively
You should know that ar
is not defined when you are trying to perform an assignment like ar[i][j] = int(input())
, there are many ways to fix that.
In C/C++, I presume you would do such work like this:
#include <cstdio>
int main()
{
int m, n;
scanf("%d %d", &m, &n);
int **ar = new int*[m];
for(int i = 0; i < m; i++)
ar[i] = new int[n];
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
scanf("%d", &ar[i][j]);
// Do what you want to do
for(int i = 0; i < m; i++)
delete ar[i];
delete ar;
return 0;
}
Before you get inputs by scanf
in C/C++, you should allocate storage by calling new
or malloc
, then you can perform your scanf
, or it will crash.
It's very similar to what you had done in C/C++, according to your code, when you are trying to perform assignment to ar[i][j]
, Python has no idea what ar
it is! So you have to let it know first.
A NOT-Pythonic way is do something like you did in C/C++:
n = int(input())
m = int(input())
ar = []
for i in range(m):
ar.append([])
for j in range(n):
k = int(input())
ar[i].append(k)
for i in range(m):
for j in range(n):
print(ar[i][j])
You initialize the list by ar = []
like you did int **ar = new int*[m];
in C/C++. For each row in the 2-d list, initialize the row by using ar.append([])
like you did ar[i] = new int[n];
in C/C++. Then, get your data by using input
and append it to ar[i]
.
The way to perform such a job like above it's not very pythonic, instead, you can get it done by using a feature called List Comprehensions, then the code can be simplified into this:
n = int(input())
m = int(input())
ar = [[0 for j in range(n)] for i in range(m)]
for i in range(m):
for j in range(n):
k = int(input())
ar[i][j] = k
for i in range(m):
for j in range(n):
print(ar[i][j])
Note that the core ar = [[0 for j in range(n)] for i in range(m)]
is a list comprehension that it creates a list which has m
lists and for each list of these m
lists it has n
0s.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With