+ 2

Can I create 2d or 3d matrices just by declaring it's elements one by 1 in loop like a[i][j] by looping I and j? (In python)

25th Dec 2017, 4:17 AM
Shreyas Ganapathï
Shreyas Ganapathï - avatar
2 Réponses
+ 1
I think so. What’s the context? In Python I think when you create a list, it starts as an empty list. So you would be appending, not assigning, in the loop: a = [] i = 0 while i < 10: a.append([]) j = 0 while j < 5: a[i].append(input()) j += 1 i += 1
25th Dec 2017, 5:07 AM
Alexander
Alexander - avatar
+ 7
I would recommend nested loops. First for first dimension, second - for second and so on.
25th Dec 2017, 5:19 AM
Freezemage
Freezemage - avatar