Question: Why isn't this function pure?

A forum for general discussion of the Python programming language.

Question: Why isn't this function pure?

Postby luishp » Wed Feb 05, 2014 12:50 am

In the following function, for some reason the last test fails and I don't know why. I made a clone of "matrix" and called it new_matrix. In my for loop I only used new_matrix. But for some reason the original function was also changed. Could someone please help me out?

Code: Select all
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]]
    """
    new_matrix = matrix[:]
    for i in new_matrix:
        i += [0]
    return new_matrix
Last edited by micseydel on Wed Feb 05, 2014 2:04 am, edited 1 time in total.
Reason: Code tags, lock.
luishp
 
Posts: 13
Joined: Wed Feb 05, 2014 12:44 am

Re: Question: Why isn't this function pure?

Postby micseydel » Wed Feb 05, 2014 2:25 am

When you use the slice, you're making a copy of the most upper list, which is good, but your newly copied list contains references to all the old lists. You need to make copies of those too. I would use copy.deepcopy() for that although you could use slicing again at the next level if this is for school and you're prohibited from using that.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1228
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Question: Why isn't this function pure?

Postby stranac » Wed Feb 05, 2014 9:08 am

If this is not for school or something like that, I would just use a list comprehension:
Code: Select all
>>> def add_column(matrix):
...     return [row + [0] for row in 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]]
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1097
Joined: Thu Feb 07, 2013 3:42 pm

Re: Question: Why isn't this function pure?

Postby luishp » Wed Feb 05, 2014 3:08 pm

This look so cool! Unfortunately I haven't learned it yet. But this in mind for a definite must learn, thank you.

stranac wrote:If this is not for school or something like that, I would just use a list comprehension:
Code: Select all
>>> def add_column(matrix):
...     return [row + [0] for row in 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]]
luishp
 
Posts: 13
Joined: Wed Feb 05, 2014 12:44 am

Re: Question: Why isn't this function pure?

Postby luishp » Wed Feb 05, 2014 3:14 pm

By using slicing again at the next level do you mean making another copy of copy I already made? I tried doing this but it still changes the original. I'm learning Python independently using openbookproject.net. deepcopy() was suggested. I'll check it out, Thanks.
Code: Select all
matrix_clone = matrix[:]
matrix_clone2 = matrix_clone[:]


micseydel wrote:When you use the slice, you're making a copy of the most upper list, which is good, but your newly copied list contains references to all the old lists. You need to make copies of those too. I would use copy.deepcopy() for that although you could use slicing again at the next level if this is for school and you're prohibited from using that.
Last edited by stranac on Wed Feb 05, 2014 6:26 pm, edited 1 time in total.
Reason: Fixed code tags.
luishp
 
Posts: 13
Joined: Wed Feb 05, 2014 12:44 am


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests