[BangPypers] BangPypers Digest, Vol 22, Issue 9
Aman Aggarwal
aman.coe at gmail.com
Fri Jun 12 08:25:26 CEST 2009
On Thu, Jun 11, 2009 at 3:30 PM, <bangpypers-request at python.org> wrote:
> Send BangPypers mailing list submissions to
> bangpypers at python.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://mail.python.org/mailman/listinfo/bangpypers
> or, via email, send a message with subject or body 'help' to
> bangpypers-request at python.org
>
> You can reach the person managing the list at
> bangpypers-owner at python.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of BangPypers digest..."
>
>
> Today's Topics:
>
> 1. [New bie question ] Clarification on "Multiply" operator
> applied to list(data structure) (Aman Aggarwal)
> 2. Re: [New bie question ] Clarification on "Multiply" operator
> applied to list(data structure) (Senthil Kumaran)
> 3. Re: [New bie question ] Clarification on "Multiply" operator
> applied to list(data structure) (Shivaraj M S)
> 4. Re: [New bie question ] Clarification on "Multiply" operator
> applied to list(data structure) (Shivaraj M S)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Wed, 10 Jun 2009 16:57:39 +0530
> From: Aman Aggarwal <aman.coe at gmail.com>
> To: bangpypers <bangpypers at python.org>
> Subject: [BangPypers] [New bie question ] Clarification on "Multiply"
> operator applied to list(data structure)
> Message-ID:
> <882494550906100427x1a99ac60h9540f04cc656a13e at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> Hello there
>
> I am reading?"How to think like a computer scientist"?which is an
> introductory test in Python.
>
> I wanna clarify the behaviour of multiply operator(*) when applied to
> list(data structure).
>
> Consider the function?make_matrix
>
> def make_matrix(rows, columns):
> """
> ? >>> make_matrix(4, 2)
> ? [[0, 0], [0, 0], [0, 0], [0, 0]]
> ? >>> m = make_matrix(4, 2)
> ? >>> m[1][1] = 7
> ? >>> m
> ? [[0, 0], [0, 7], [0, 0], [0, 0]]
> """
> return [[0] * columns] * rows
>
>
>
>
> The actual output is
>
> [[0, 7], [0, 7], [0, 7], [0, 7]]
>
>
>
>
> The correct version of?make_matrix?is :
>
>
>
> def make_matrix(rows, columns):
> """
> ? >>> make_matrix(3, 5)
> ? [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
> ? >>> make_matrix(4, 2)
> ? [[0, 0], [0, 0], [0, 0], [0, 0]]
> ? >>> m = make_matrix(4, 2)
> ? >>> m[1][1] = 7
> ? >>> m
> ? [[0, 0], [0, 7], [0, 0], [0, 0]]
> """
> matrix = []
> for row in range(rows):
> ? ? matrix += [[0] * columns]
> return matrix
>
>
> The reason why first version of?make_matrix?fails ( as explained in
> the book at 9.8 ) is that
>
> "...each row is an alias of the other rows..."
>
> I wonder why
>
> [[0] * columns] * rows
>
> causes?"...each row is an alias of the other rows..."
>
> but not
>
> [[0] * columns]
>
> i.e. why each [0] in a row is not an alias of other row element.
>
>
>
>
>
> /*
> ?Everything worth doing is worth doing in excess
> */
>
>
> ------------------------------
>
> Message: 2
> Date: Wed, 10 Jun 2009 19:45:20 +0530
> From: Senthil Kumaran <orsenthil at gmail.com>
> To: Bangalore Python Users Group - India <bangpypers at python.org>
> Subject: Re: [BangPypers] [New bie question ] Clarification on
> "Multiply" operator applied to list(data structure)
> Message-ID: <20090610141520.GB14638 at ubuntu.ubuntu-domain>
> Content-Type: text/plain; charset=iso-8859-1
>
> On Wed, Jun 10, 2009 at 04:57:39PM +0530, Aman Aggarwal wrote:
>>
>> I am reading?"How to think like a computer scientist"?which is an
>> introductory test in Python.
>>
>> I wanna clarify the behaviour of multiply operator(*) when applied to
>> list(data structure).
>>
>>
>> I wonder why
>>
>> [[0] * columns] * rows
>>
>> causes?"...each row is an alias of the other rows..."
>>
>> but not
>>
>> [[0] * columns]
>>
>> i.e. why each [0] in a row is not an alias of other row element.
>
>
> It is bit tricky in that code..
> You have to understand that in the second option, the matrix is formed
> row-by-row, wherein a new row (list) is created as [0] * columns.
>
>
> This is what the second version essentially is doing:
>
>>>> rows = 2
>>>> cols = 2
>>>> mat = []
>>>> mat = [[0] * cols] # first row
>>>> mat = mat + [[0] * cols] # second row
>>>> mat
> [[0, 0], [0, 0]]
>>>> mat[0][0] = 'Spam'
>>>> mat
> [['Spam', 0], [0, 0]]
>>>>
>
> This is the what the first version of the code was doing.
>
>>>> rows = 2
>>>> cols = 2
>>>> mat = []
>>>> mat = [[0] * cols] # first row
>>>> mat = mat * 2 # second row
>>>> mat
> [[0, 0], [0, 0]]
>>>> mat[0][0] = 'Spam'
>>>> mat
> [['Spam', 0], ['Spam', 0]]
>>>>
>
> Does this clarify your doubt?
>
> --
> Senthil
>
>
>
> ------------------------------
>
> Message: 3
> Date: Wed, 10 Jun 2009 06:07:27 -0700 (PDT)
> From: Shivaraj M S <shivraj.ms at gmail.com>
> To: bangpypers at python.org
> Subject: Re: [BangPypers] [New bie question ] Clarification on
> "Multiply" operator applied to list(data structure)
> Message-ID: <23961937.post at talk.nabble.com>
> Content-Type: text/plain; charset=UTF-8
>
>
> The first one is a reference and second one is not.
> case 1:
>>>> m = [[0]*2]*4
>>>> m[1][1]=7
>>>> m
> [[0, 7], [0, 7], [0, 7], [0, 7]]
>
> case 2:
>>>> m=[]
>>>> for r in range(4):
> ... m+=[[0]*2]
> ...
>>>> m
> [[0, 0], [0, 0], [0, 0], [0, 0]]
>>>> m[1][1]=7
>>>> m
> [[0, 0], [0, 7], [0, 0], [0, 0]]
>
> We can make the second case look like first just by creating a reference to
> [[0]*2] which is what was done implicitly when multiplication operator was
> used.
>
> case 2 - modified for reference:
>>>> m=[]
>>>> mi = [[0]*2]
>>>> for r in range(4):
> ... m+=mi
> ...
>>>> m
> [[0, 0], [0, 0], [0, 0], [0, 0]]
>>>> mi[0][1]=7
>>>> m
> [[0, 7], [0, 7], [0, 7], [0, 7]]
>>>> m[1][1]=1
>>>> m
> [[0, 1], [0, 1], [0, 1], [0, 1]]
>>>> mi
> [[0, 1]]
>
>
>
>
> Aman Aggarwal-4 wrote:
>>
>> Hello there
>>
>> I am reading?"How to think like a computer scientist"?which is an
>> introductory test in Python.
>>
>> I wanna clarify the behaviour of multiply operator(*) when applied to
>> list(data structure).
>>
>> Consider the function?make_matrix
>>
>> def make_matrix(rows, columns):
>> """
>> ? >>> make_matrix(4, 2)
>> ? [[0, 0], [0, 0], [0, 0], [0, 0]]
>> ? >>> m = make_matrix(4, 2)
>> ? >>> m[1][1] = 7
>> ? >>> m
>> ? [[0, 0], [0, 7], [0, 0], [0, 0]]
>> """
>> return [[0] * columns] * rows
>>
>>
>>
>>
>> The actual output is
>>
>> [[0, 7], [0, 7], [0, 7], [0, 7]]
>>
>>
>>
>>
>> The correct version of?make_matrix?is :
>>
>>
>>
>> def make_matrix(rows, columns):
>> """
>> ? >>> make_matrix(3, 5)
>> ? [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
>> ? >>> make_matrix(4, 2)
>> ? [[0, 0], [0, 0], [0, 0], [0, 0]]
>> ? >>> m = make_matrix(4, 2)
>> ? >>> m[1][1] = 7
>> ? >>> m
>> ? [[0, 0], [0, 7], [0, 0], [0, 0]]
>> """
>> matrix = []
>> for row in range(rows):
>> ? ? matrix += [[0] * columns]
>> return matrix
>>
>>
>> The reason why first version of?make_matrix?fails ( as explained in
>> the book at 9.8 ) is that
>>
>> "...each row is an alias of the other rows..."
>>
>> I wonder why
>>
>> [[0] * columns] * rows
>>
>> causes?"...each row is an alias of the other rows..."
>>
>> but not
>>
>> [[0] * columns]
>>
>> i.e. why each [0] in a row is not an alias of other row element.
>>
>>
>>
>>
>>
>> /*
>> ?Everything worth doing is worth doing in excess
>> */
>> _______________________________________________
>> BangPypers mailing list
>> BangPypers at python.org
>> http://mail.python.org/mailman/listinfo/bangpypers
>>
>>
>
> --
> View this message in context: http://www.nabble.com/-New-bie-question---Clarification-on-%22Multiply%22-operator-applied-to-list%28data-structure%29-tp23960277p23961937.html
> Sent from the BangPypers - Bangalore Python Users Group mailing list archive at Nabble.com.
>
>
>
> ------------------------------
>
> Message: 4
> Date: Wed, 10 Jun 2009 06:09:16 -0700 (PDT)
> From: Shivaraj M S <shivraj.ms at gmail.com>
> To: bangpypers at python.org
> Subject: Re: [BangPypers] [New bie question ] Clarification on
> "Multiply" operator applied to list(data structure)
> Message-ID: <23961977.post at talk.nabble.com>
> Content-Type: text/plain; charset=UTF-8
>
>
> The first one is a case of reference/alias and second one is not.
> case 1:
>>>> m = [[0]*2]*4
>>>> m[1][1]=7
>>>> m
> [[0, 7], [0, 7], [0, 7], [0, 7]]
>
> case 2:
>>>> m=[]
>>>> for r in range(4):
> ... m+=[[0]*2]
> ...
>>>> m
> [[0, 0], [0, 0], [0, 0], [0, 0]]
>>>> m[1][1]=7
>>>> m
> [[0, 0], [0, 7], [0, 0], [0, 0]]
>
> We can make the second case look like first just by creating a reference to
> [[0]*2] which is what was done implicitly when multiplication operator was
> used.
>
> case 2 - modified for reference:
>>>> m=[]
>>>> mi = [[0]*2]
>>>> for r in range(4):
> ... m+=mi
> ...
>>>> m
> [[0, 0], [0, 0], [0, 0], [0, 0]]
>>>> mi[0][1]=7
>>>> m
> [[0, 7], [0, 7], [0, 7], [0, 7]]
>>>> m[1][1]=1
>>>> m
> [[0, 1], [0, 1], [0, 1], [0, 1]]
>>>> mi
> [[0, 1]]
>
>
>
>
> Aman Aggarwal-4 wrote:
>>
>> Hello there
>>
>> I am reading?"How to think like a computer scientist"?which is an
>> introductory test in Python.
>>
>> I wanna clarify the behaviour of multiply operator(*) when applied to
>> list(data structure).
>>
>> Consider the function?make_matrix
>>
>> def make_matrix(rows, columns):
>> """
>> ? >>> make_matrix(4, 2)
>> ? [[0, 0], [0, 0], [0, 0], [0, 0]]
>> ? >>> m = make_matrix(4, 2)
>> ? >>> m[1][1] = 7
>> ? >>> m
>> ? [[0, 0], [0, 7], [0, 0], [0, 0]]
>> """
>> return [[0] * columns] * rows
>>
>>
>>
>>
>> The actual output is
>>
>> [[0, 7], [0, 7], [0, 7], [0, 7]]
>>
>>
>>
>>
>> The correct version of?make_matrix?is :
>>
>>
>>
>> def make_matrix(rows, columns):
>> """
>> ? >>> make_matrix(3, 5)
>> ? [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
>> ? >>> make_matrix(4, 2)
>> ? [[0, 0], [0, 0], [0, 0], [0, 0]]
>> ? >>> m = make_matrix(4, 2)
>> ? >>> m[1][1] = 7
>> ? >>> m
>> ? [[0, 0], [0, 7], [0, 0], [0, 0]]
>> """
>> matrix = []
>> for row in range(rows):
>> ? ? matrix += [[0] * columns]
>> return matrix
>>
>>
>> The reason why first version of?make_matrix?fails ( as explained in
>> the book at 9.8 ) is that
>>
>> "...each row is an alias of the other rows..."
>>
>> I wonder why
>>
>> [[0] * columns] * rows
>>
>> causes?"...each row is an alias of the other rows..."
>>
>> but not
>>
>> [[0] * columns]
>>
>> i.e. why each [0] in a row is not an alias of other row element.
>>
>>
>>
>>
>>
>> /*
>> ?Everything worth doing is worth doing in excess
>> */
>> _______________________________________________
>> BangPypers mailing list
>> BangPypers at python.org
>> http://mail.python.org/mailman/listinfo/bangpypers
>>
>>
>
> --
> View this message in context: http://www.nabble.com/-New-bie-question---Clarification-on-%22Multiply%22-operator-applied-to-list%28data-structure%29-tp23960277p23961977.html
> Sent from the BangPypers - Bangalore Python Users Group mailing list archive at Nabble.com.
>
>
>
> ------------------------------
>
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
>
> End of BangPypers Digest, Vol 22, Issue 9
> *****************************************
>
Hi there
thanks for the response.
I posted the same question here :
http://stackoverflow.com/questions/974931/multiply-operator-applied-to-listdata-structure
Kindly read it.
Thanks and kind regards
Aman
More information about the BangPypers
mailing list