There is an integer matrix. It is necessary to define a column with the minimum product of elements. My code is:

import random n=int(input('Введите кол-во строк матрицы: ')) m=int(input('Введите кол-во столбцов матрицы: ')) matrix=[[random.randrange(10) for i in range(n)] for j in range(m)] for elem in matrix: print(elem) 

But how to subtract the product of the elements of the matrix and compare with the subsequent columns? help me please

    1 answer 1

    The easiest way to do this is by transposing the matrix:

     from functools import reduce import operator matrix = [[6, 2, 1, 4, 4], [8, 5, 6, 6, 1], [7, 7, 6, 5, 8]] res = min(zip(*matrix), key=lambda r: reduce(lambda x, y: x*y, r)) print(res) # (4, 1, 8) 

    if you need a column index:

     In [56]: res = min(enumerate(zip(*matrix)), key=lambda t: reduce(lambda x, y: x*y, t[1])) In [57]: res Out[57]: (4, (4, 1, 8)) In [58]: res[0] Out[58]: 4