Tasks: Fill the elements of the matrix randomly with integers. Print the elements of the matrix. Print the number of columns whose sum of elements is the largest

In python recently, and the language is not very like but practical need to pass.

import random def creatArray(): print('Input first index matrix: ') x = int(input()) print('Input second index matrix: ') y = int(input()) array = ([0]*x)*y for i in range (0,x-1): for j in range (0, y-1): array[i][j] = random.randint(0,100) return array print(creatArray()) 

this way I wanted to fill the array, but gives an error on line 14 and 11. 'TypeError: 'int' object does not support item assignment

Please explain what the problem is and help with the solution with 3 tasks (print the number of columns ...)

  • Specify the content of the problem line - Vladimir Martyanov

3 answers 3

Numpy style solution :

 In [9]: import numpy as np In [10]: nrows = 10 In [11]: ncols = 8 In [12]: a = np.random.randint(100, size=(nrows,ncols)) In [13]: a Out[13]: array([[42, 18, 97, 96, 19, 98, 74, 1], [ 1, 23, 81, 83, 29, 41, 59, 57], [ 9, 11, 13, 37, 31, 54, 51, 38], [42, 14, 30, 91, 2, 89, 86, 70], [60, 87, 78, 63, 34, 29, 24, 82], [74, 61, 36, 26, 78, 28, 45, 36], [67, 31, 75, 8, 60, 14, 9, 76], [71, 60, 82, 55, 81, 89, 23, 42], [12, 76, 83, 11, 17, 54, 65, 50], [37, 63, 42, 79, 10, 32, 43, 53]]) 

sum of items in columns:

 In [14]: a.sum(axis=0) Out[14]: array([415, 444, 617, 549, 361, 528, 479, 505]) 

column with the maximum sum of items:

 In [15]: a.sum(axis=0).argmax() Out[15]: 2 

sum of items in rows:

 In [16]: a.sum(axis=1) Out[16]: array([445, 374, 244, 424, 457, 384, 340, 503, 368, 359]) 

line with the maximum sum of elements:

 In [17]: a.sum(axis=1).argmax() Out[17]: 7 
  • Thank you very much for your answer! - 4aki

([0]*x)*y is not a list of lists, but simply a list of integers: [0] * x expands to a list of zeros, and then [0, ..., 0] * y turns into a list of x * y zeros. Attempting to access zero as a list results in the indicated error.

To correctly create a list of lists consisting entirely of zeros, you can use the list generator:

 array = [[0] * x for _ in range(y)] 

You can also use a two-level list generator to immediately create a list of lists with random numbers:

 array = [[random.randint(0, 100) for i in range(x)] for j in range(y)] 

By the way, because range(0, x-1) gives the numbers from 0 to x-2 , then you have zeroes in the array in the last column and the last row. For it to work correctly, it must be range(0, x) or just range(x) .

With the rest, I would advise you to sort it out yourself. If it does not work out at all, then ask a separate question.

  • Thank you very much for your answer! - 4aki

Thank you all, but found another option that came up to me

 import random def creatArray(): r = 0 print('Input first index matrix: ') x = int(input()) print('Input second index matrix: ') y = int(input()) array = [] for i in range(x): array.append([]) for j in range(y): array[i].append(random.randint(0,100)) r += 1 return array print(creatArray())