N
N
newfolder_js2022-04-20 13:45:19
Python
newfolder_js, 2022-04-20 13:45:19

How to enter an array in Python like in c++??

switched from c++;
it's easier there

for (i=0;i<n;++)
for (j=0;j<m;++)
cin>>a[i][j];

and all you can do is call a[3][2] to get element 3 row 2 column
here I have an error
n=int(input())
m=int(input())
num = [[int(input()) for i in range(n)] for j in range(m)]
for i in range(0,n):
    print("\n")
    for j in range(0,m):
        print(num[i][j], end='   ')

IndexError: list index out of range
print(num[i][j], end='   ')

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
A
Alexander Nesterov, 2022-04-20
@newfolder_js

Swap n/m in cycles.
Better yet, iterate over the array itself.

import random
n=int(input())
m=int(input())
num = [[int(random.randint(1,6)) for i in range(n)] for j in range(m)]
for i in num:
    print("\n")
    for j in i:
        print(j, end='   ')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question