I'm very new to python, so this is very basic. I'm using the same basis i used on an add row program, which is probably why it comes up adding a row. When i read it back to myself, it seems to make sense. Thanks in advance for any help.

def add_column(matrix):
    """
      >>> m = [[0, 0], [0, 0]]
      >>> add_column(m)
      [[0, 0, 0], [0, 0, 0]]
      >>> n = [[3, 2], [5, 1], [4, 7]]
      >>> add_column(n)
      [[3, 2, 0], [5, 1, 0], [4, 7, 0]]
      >>> n
      [[3, 2], [5, 1], [4, 7]]
    """

    rows=len(matrix)
    columns=len(matrix[1])
    new_matrix = []
    for columns in range(rows):
        new_matrix = matrix + [(1 * [0]) * rows] 
    return new_matrix

if __name__ == '__main__':
    import doctest
    doctest.testmod()

this is what comes up when i run it:

>>> ================================ RESTART ================================
>>> 
**********************************************************************
File "__main__", line 4, in __main__.add_column
Failed example:
    add_column(m)
Expected:
    [[0, 0, 0], [0, 0, 0]]
Got:
    [[0, 0], [0, 0], [0, 0]]
**********************************************************************
File "__main__", line 7, in __main__.add_column
Failed example:
    add_column(n)
Expected:
    [[3, 2, 0], [5, 1, 0], [4, 7, 0]]
Got:
    [[3, 2], [5, 1], [4, 7], [0, 0, 0]]
**********************************************************************
1 items had failures:
   2 of   5 in __main__.add_column
***Test Failed*** 2 failures.
>>> *******************************************

Recommended Answers

All 3 Replies

You are over-thinking this

list_n = [[3, 2], [5, 1], [4, 7]]
print "Before",  list_n
for j in range(0, len(list_n)):
   row = list_n[j]
   row.append(0)   ## can also use just list_n[j].append(0)
print "After ", list_n

I'm very new to python, so this is very basic. I'm using the same basis i used on an add row program, which is probably why it comes up adding a row. When i read it back to myself, it seems to make sense. Thanks in advance for any help.

def add_column(matrix):
    """
      >>> m = [[0, 0], [0, 0]]
      >>> add_column(m)
      [[0, 0, 0], [0, 0, 0]]
      >>> n = [[3, 2], [5, 1], [4, 7]]
      >>> add_column(n)
      [[3, 2, 0], [5, 1, 0], [4, 7, 0]]
      >>> n
      [[3, 2], [5, 1], [4, 7]]
    """

    rows=len(matrix)
    columns=len(matrix[1])
    new_matrix = []
    for columns in range(rows):
        new_matrix = matrix + [(1 * [0]) * rows] 
    return new_matrix

if __name__ == '__main__':
    import doctest
    doctest.testmod()

this is what comes up when i run it:

>>> ================================ RESTART ================================
>>> 
**********************************************************************
File "__main__", line 4, in __main__.add_column
Failed example:
    add_column(m)
Expected:
    [[0, 0, 0], [0, 0, 0]]
Got:
    [[0, 0], [0, 0], [0, 0]]
**********************************************************************
File "__main__", line 7, in __main__.add_column
Failed example:
    add_column(n)
Expected:
    [[3, 2, 0], [5, 1, 0], [4, 7, 0]]
Got:
    [[3, 2], [5, 1], [4, 7], [0, 0, 0]]
**********************************************************************
1 items had failures:
   2 of   5 in __main__.add_column
***Test Failed*** 2 failures.
>>> *******************************************

Note the usage of code tags and icode tags are as follows:
[code=python] # Block of code goes here

[/code]
For inline code you'd use [icode] # Short code statement [/icode]

I have to pass these doctests. When i do try and run this, it doesn't pass them. Sorry if i am being difficult.

def add_column(matrix):
    """
      >>> m = [[0, 0], [0, 0]]
      >>> add_column(m)
      [[0, 0, 0], [0, 0, 0]]
      >>> n = [[3, 2], [5, 1], [4, 7]]
      >>> add_column(n)
      [[3, 2, 0], [5, 1, 0], [4, 7, 0]]
      >>> n
      [[3, 2], [5, 1], [4, 7]]
    """
    list_n = matrix
    print "Before",  list_n
    for j in range(0, len(list_n)):
        row = list_n[j]
        row.append(0) 
if __name__ == '__main__':
    import doctest
    doctest.testmod()

This is what i am returned when i run it.

>>> ================================ RESTART ================================
>>> 
**********************************************************************
File "__main__", line 4, in __main__.add_column
Failed example:
    add_column(m)
Expected:
    [[0, 0, 0], [0, 0, 0]]
Got:
    Before [[0, 0], [0, 0]]
**********************************************************************
File "__main__", line 7, in __main__.add_column
Failed example:
    add_column(n)
Expected:
    [[3, 2, 0], [5, 1, 0], [4, 7, 0]]
Got:
    Before [[3, 2], [5, 1], [4, 7]]
**********************************************************************
File "__main__", line 9, in __main__.add_column
Failed example:
    n
Expected:
    [[3, 2], [5, 1], [4, 7]]
Got:
    [[3, 2, 0], [5, 1, 0], [4, 7, 0]]
**********************************************************************
1 items had failures:
   3 of   5 in __main__.add_column
***Test Failed*** 3 failures.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.