I perform university tasks, there are a lot of tasks, where input looks like this:

Дан массив, в нём, например, пять столбцов и N строк. Посчитайте сумму всех значений. a1: 1 2 3 4 5 a2: 6 7 8 9 10 a3: 11 12 13 14 15 ... an: 9 5 4 6 4 

The question is how to properly fill such an array with input ()? What should a loop look like for a multidimensional array?

  • Since this is a training task, you should give your code and / or reasoning about how to do it, and what is the problem with you. - Timofei Bondarev
  • Just like without input . - LEQADA
  • one
    Proceed from the fact that a two-dimensional array can be represented as a list of lists: dbl_arr = [ [1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15], ..., [9,5,4,6,4] ] - aleks.andr

2 answers 2

In general, I seem to have found a more or less correct solution:

 b = [list(map(int, input().split())) for i in range(n)] 

So I got what I wanted. For example, when n = 3, the array looks like this:

 [[1, 2, 3, 4, 5], [6, 7, 8, 9, 0], [5, 6, 7, 8, 9]] 

But it seems to me that this can be done a little easier, until I know how.

  • under the link which I resulted there is absolutely simple solution: a = numpy.loadtxt(sys.stdin) . If you cannot use other libraries, then a = [list(map(int, line.split())) for line in sys.stdin] (Python 3) is an acceptable code. - jfs

You can also go through the dictionary! then it's easier to count the amount

 l={} for i in range(1,6): for j in range(1,n): l[i,j] = int(input())