Home > Net >  can anyone help in solving this python problem
can anyone help in solving this python problem

Time:11-12

Write a function, empty_matrix, that takes two arguments

  1. An integer (which represents the length of sequence one 1).
  2. An integer (which represents the length of sequence two 1). The function must return: • A list of lists. The number of sub-lists be must equal to the first integer argument. Each sublist must contain a number of None values equal to the second integer argument. Example usage: empty_matrix(3, 4) returns a list with three lists each of length four: [[None, None, None, None], [None, None, None, None], [None, None, None, None]] Even though this is a list of lists we can think of it as a three by four matrix:
[[None, None, None, None],
 [None, None, None, None],
 [None, None, None, None]]

CodePudding user response:

def empty_matrix(childListCount,childElementCount):
    matrix = []
    for X in range(0,childListCount):
        localList = []
        for Y in range(0,childElementCount):
            localList.append(None)
        matrix.append(localList)
    return matrix

print(empty_matrix(3,4))

CodePudding user response:

def empty_matrix(outer_length, inner_length):
    inner = inner_length * [None]
    final = outer_length * [inner]
    return final

If you work with matrices you may also want to look at numpy

  • Related