Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #30235 > unrolled thread
| Started by | TP <TP@frenoespam.fr.invalid> |
|---|---|
| First post | 2012-09-26 23:20 +0200 |
| Last post | 2012-09-26 14:45 -0700 |
| Articles | 19 — 7 participants |
Back to article view | Back to comp.lang.python
using "*" to make a list of lists with repeated (and independent) elements TP <TP@frenoespam.fr.invalid> - 2012-09-26 23:20 +0200
Re: using "*" to make a list of lists with repeated (and independent) elements Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-26 15:39 -0600
Re: using "*" to make a list of lists with repeated (and independent) elements Paul Rubin <no.email@nospam.invalid> - 2012-09-26 14:43 -0700
Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-26 15:07 -0700
Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-26 15:28 -0700
Re: using "*" to make a list of lists with repeated (and independent) elements Tim Chase <python.list@tim.thechases.com> - 2012-09-26 17:45 -0500
Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-26 15:53 -0700
Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-26 15:53 -0700
Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-27 14:24 -0700
Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-27 14:24 -0700
Re: using "*" to make a list of lists with repeated (and independent) elements Ramchandra Apte <maniandram01@gmail.com> - 2012-09-29 06:46 -0700
Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-29 10:01 -0700
Re: using "*" to make a list of lists with repeated (and independent) elements Ian Kelly <ian.g.kelly@gmail.com> - 2012-09-29 11:18 -0600
Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-29 11:50 -0700
Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-29 11:50 -0700
Re: using "*" to make a list of lists with repeated (and independent) elements Chris Angelico <rosuav@gmail.com> - 2012-09-30 03:41 +1000
Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-29 10:01 -0700
Re: using "*" to make a list of lists with repeated (and independent) elements Ramchandra Apte <maniandram01@gmail.com> - 2012-09-29 06:46 -0700
Re: using "*" to make a list of lists with repeated (and independent) elements 88888 Dihedral <dihedral88888@googlemail.com> - 2012-09-26 14:45 -0700
| From | TP <TP@frenoespam.fr.invalid> |
|---|---|
| Date | 2012-09-26 23:20 +0200 |
| Subject | using "*" to make a list of lists with repeated (and independent) elements |
| Message-ID | <b58cj9-h5d.ln1@rama.fbx.proxad.net> |
Hi everybody, I have tried, naively, to do the following, so as to make lists quickly: >>> a=[0]*2 >>> a [0, 0] >>> a[0]=3 >>> a [3, 0] All is working fine, so I extended the technique to do: >>> a=[[0]*3]*2 >>> a [[0, 0, 0], [0, 0, 0]] >>> a[0][0]=2 >>> a [[2, 0, 0], [2, 0, 0]] The behavior is no more expected! The reason is probably that in the first case, 0 is an integer, not a list, so Python copies two elements that are independent. In the second case, the elements are [0,0,0], which is a list; when Python copies a list, he copies in fact the *pointer* to the list, such that we obtain this apparently strange behavior. Is it the correct explanation? In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*" without this behavior? Thanks, TP
[toc] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-09-26 15:39 -0600 |
| Message-ID | <mailman.1458.1348695604.27098.python-list@python.org> |
| In reply to | #30235 |
On Wed, Sep 26, 2012 at 3:20 PM, TP <TP@frenoespam.fr.invalid> wrote: > Hi everybody, > > I have tried, naively, to do the following, so as to make lists quickly: > >>>> a=[0]*2 >>>> a > [0, 0] >>>> a[0]=3 >>>> a > [3, 0] > > All is working fine, so I extended the technique to do: > >>>> a=[[0]*3]*2 >>>> a > [[0, 0, 0], [0, 0, 0]] >>>> a[0][0]=2 >>>> a > [[2, 0, 0], [2, 0, 0]] > > The behavior is no more expected! > The reason is probably that in the first case, 0 is an integer, not a list, > so Python copies two elements that are independent. > In the second case, the elements are [0,0,0], which is a list; when Python > copies a list, he copies in fact the *pointer* to the list, such that we > obtain this apparently strange behavior. Mostly correct. When you do [foo] * 3 it extends the list with the *same objects* no matter what type they are. In the case of integers, it doesn't matter that it's the same objects, because integers are immutable. Lists are mutable, however, and so it becomes apparent that the same objects are repeated when you try to modify one of the lists. > In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*" > without this behavior? Use a list comprehension: a = [[0] * 3 for _ in range(2)] This way the expression `[0] * 3` is re-evaluated at each position in the outer list, rather than evaluated just once and then copied.
[toc] | [prev] | [next] | [standalone]
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Date | 2012-09-26 14:43 -0700 |
| Message-ID | <7x7grgtq75.fsf@ruckus.brouhaha.com> |
| In reply to | #30235 |
TP <TP@frenoespam.fr.invalid> writes:
> copies a list, he copies in fact the *pointer* to the list ....
> Is it the correct explanation?
Yes, that is correct.
> In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*"
> without this behavior?
>>> a = [[0]*3 for i in xrange(2)]
>>> a[0][0]=2
>>> a
[[2, 0, 0], [0, 0, 0]]
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-09-26 15:07 -0700 |
| Message-ID | <f7dd2519-4e27-4905-805e-1ecc0b06b06f@googlegroups.com> |
| In reply to | #30237 |
Paul Rubin於 2012年9月27日星期四UTC+8上午5時43分58秒寫道: > TP <TP@frenoespam.fr.invalid> writes: > > > copies a list, he copies in fact the *pointer* to the list .... > > > Is it the correct explanation? > > > > Yes, that is correct. > > > > > In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*" > > > without this behavior? > > > > >>> a = [[0]*3 for i in xrange(2)] > > >>> a[0][0]=2 > > >>> a > > [[2, 0, 0], [0, 0, 0]] I used numpy before. Python is not lisp but python can emulate the lisp behaviors.
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-09-26 15:28 -0700 |
| Message-ID | <4657ccef-b910-4781-9ea8-005b4e15f219@googlegroups.com> |
| In reply to | #30241 |
88888 Dihedral於 2012年9月27日星期四UTC+8上午6時07分35秒寫道: > Paul Rubin於 2012年9月27日星期四UTC+8上午5時43分58秒寫道: > > > TP <TP@frenoespam.fr.invalid> writes: > > > > > > > copies a list, he copies in fact the *pointer* to the list .... > > > > > > > Is it the correct explanation? > > > > > > > > > > > > Yes, that is correct. > > > > > > > > > > > > > In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*" > > > > > > > without this behavior? > > > > > > > > > > > > >>> a = [[0]*3 for i in xrange(2)] > > > > > > >>> a[0][0]=2 > > > > > > >>> a > > > > > > [[2, 0, 0], [0, 0, 0]] > > > > I used numpy before. > > > > Python is not lisp but python can emulate the lisp behaviors. def zeros(m,n): a=[] for i in xrange(m): a.append([0]*n) return a If one wants to tranlate to C, this is the style.
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2012-09-26 17:45 -0500 |
| Message-ID | <mailman.1461.1348699454.27098.python-list@python.org> |
| In reply to | #30242 |
On 09/26/12 17:28, 88888 Dihedral wrote: > 88888 Dihedral於 2012年9月27日星期四UTC+8上午6時07分35秒寫道: >>>> In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*" >>>> without this behavior? >>> >>> a = [[0]*3 for i in xrange(2)] >>> >>> a[0][0]=2 >>> >>> a >>> [[2, 0, 0], [0, 0, 0]] > > def zeros(m,n): > a=[] > for i in xrange(m): > a.append([0]*n) > return a > > If one wants to tranlate to C, this is the style. But this is Python, so why the heck would anybody want to emulate *C* style? It could also be written in an assembly-language style, COBOL style, or a Fortran style...none of which are particularly valuable. Besides, a C-style would allocate a single array of M*N slots and then calculate 2d offsets into that single array. :-P -tkc
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-09-26 15:53 -0700 |
| Message-ID | <37c60fca-449c-49d4-b64b-3a50e10b5007@googlegroups.com> |
| In reply to | #30243 |
Tim Chase於 2012年9月27日星期四UTC+8上午6時44分42秒寫道: > On 09/26/12 17:28, 88888 Dihedral wrote: > > > 88888 Dihedral於 2012年9月27日星期四UTC+8上午6時07分35秒寫道: > > >>>> In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*" > > >>>> without this behavior? > > >>> >>> a = [[0]*3 for i in xrange(2)] > > >>> >>> a[0][0]=2 > > >>> >>> a > > >>> [[2, 0, 0], [0, 0, 0]] > > > > > > def zeros(m,n): > > > a=[] > > > for i in xrange(m): > > > a.append([0]*n) > > > return a > > > > > > If one wants to tranlate to C, this is the style. > > > > But this is Python, so why the heck would anybody want to emulate > > *C* style? It could also be written in an assembly-language style, > > COBOL style, or a Fortran style...none of which are particularly > > valuable. > > > > Besides, a C-style would allocate a single array of M*N slots and > > then calculate 2d offsets into that single array. :-P > > > > -tkc I don't think a lot programmers can write assembly programs well for different instruction sets of cpus. Of course if GCC was not supportd in manny platforms free of charge, then I won't recommend this style of programming in python.
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-09-26 15:53 -0700 |
| Message-ID | <mailman.1462.1348700023.27098.python-list@python.org> |
| In reply to | #30243 |
Tim Chase於 2012年9月27日星期四UTC+8上午6時44分42秒寫道: > On 09/26/12 17:28, 88888 Dihedral wrote: > > > 88888 Dihedral於 2012年9月27日星期四UTC+8上午6時07分35秒寫道: > > >>>> In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*" > > >>>> without this behavior? > > >>> >>> a = [[0]*3 for i in xrange(2)] > > >>> >>> a[0][0]=2 > > >>> >>> a > > >>> [[2, 0, 0], [0, 0, 0]] > > > > > > def zeros(m,n): > > > a=[] > > > for i in xrange(m): > > > a.append([0]*n) > > > return a > > > > > > If one wants to tranlate to C, this is the style. > > > > But this is Python, so why the heck would anybody want to emulate > > *C* style? It could also be written in an assembly-language style, > > COBOL style, or a Fortran style...none of which are particularly > > valuable. > > > > Besides, a C-style would allocate a single array of M*N slots and > > then calculate 2d offsets into that single array. :-P > > > > -tkc I don't think a lot programmers can write assembly programs well for different instruction sets of cpus. Of course if GCC was not supportd in manny platforms free of charge, then I won't recommend this style of programming in python.
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-09-27 14:24 -0700 |
| Message-ID | <19af87b0-1e0e-4931-9d0e-7461303d2304@googlegroups.com> |
| In reply to | #30243 |
Tim Chase於 2012年9月27日星期四UTC+8上午6時44分42秒寫道: > On 09/26/12 17:28, 88888 Dihedral wrote: > > > 88888 Dihedral於 2012年9月27日星期四UTC+8上午6時07分35秒寫道: > > >>>> In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*" > > >>>> without this behavior? > > >>> >>> a = [[0]*3 for i in xrange(2)] > > >>> >>> a[0][0]=2 > > >>> >>> a > > >>> [[2, 0, 0], [0, 0, 0]] > > > > > > def zeros(m,n): > > > a=[] > > > for i in xrange(m): > > > a.append([0]*n) > > > return a > > > > > > If one wants to tranlate to C, this is the style. > > > > But this is Python, so why the heck would anybody want to emulate > > *C* style? It could also be written in an assembly-language style, > > COBOL style, or a Fortran style...none of which are particularly > > valuable. > > > > Besides, a C-style would allocate a single array of M*N slots and > > then calculate 2d offsets into that single array. :-P > > > > -tkc a=[1, 2,3] b=[a]*4 print b a[1]=4 print b I thnik this is very clear about the situation in entangled references.
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-09-27 14:24 -0700 |
| Message-ID | <mailman.1516.1348781049.27098.python-list@python.org> |
| In reply to | #30243 |
Tim Chase於 2012年9月27日星期四UTC+8上午6時44分42秒寫道: > On 09/26/12 17:28, 88888 Dihedral wrote: > > > 88888 Dihedral於 2012年9月27日星期四UTC+8上午6時07分35秒寫道: > > >>>> In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*" > > >>>> without this behavior? > > >>> >>> a = [[0]*3 for i in xrange(2)] > > >>> >>> a[0][0]=2 > > >>> >>> a > > >>> [[2, 0, 0], [0, 0, 0]] > > > > > > def zeros(m,n): > > > a=[] > > > for i in xrange(m): > > > a.append([0]*n) > > > return a > > > > > > If one wants to tranlate to C, this is the style. > > > > But this is Python, so why the heck would anybody want to emulate > > *C* style? It could also be written in an assembly-language style, > > COBOL style, or a Fortran style...none of which are particularly > > valuable. > > > > Besides, a C-style would allocate a single array of M*N slots and > > then calculate 2d offsets into that single array. :-P > > > > -tkc a=[1, 2,3] b=[a]*4 print b a[1]=4 print b I thnik this is very clear about the situation in entangled references.
[toc] | [prev] | [next] | [standalone]
| From | Ramchandra Apte <maniandram01@gmail.com> |
|---|---|
| Date | 2012-09-29 06:46 -0700 |
| Message-ID | <98034dee-0108-4099-bbdd-82f90e96d383@googlegroups.com> |
| In reply to | #30243 |
On Thursday, 27 September 2012 04:14:42 UTC+5:30, Tim Chase wrote: > On 09/26/12 17:28, 88888 Dihedral wrote: > > > 88888 Dihedral於 2012年9月27日星期四UTC+8上午6時07分35秒寫道: > > >>>> In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*" > > >>>> without this behavior? > > >>> >>> a = [[0]*3 for i in xrange(2)] > > >>> >>> a[0][0]=2 > > >>> >>> a > > >>> [[2, 0, 0], [0, 0, 0]] > > > > > > def zeros(m,n): > > > a=[] > > > for i in xrange(m): > > > a.append([0]*n) > > > return a > > > > > > If one wants to tranlate to C, this is the style. > > > > But this is Python, so why the heck would anybody want to emulate > > *C* style? It could also be written in an assembly-language style, > > COBOL style, or a Fortran style...none of which are particularly > > valuable. > > > > Besides, a C-style would allocate a single array of M*N slots and > > then calculate 2d offsets into that single array. :-P > > > > -tkc 88888 Dihedral is a bot.
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-09-29 10:01 -0700 |
| Message-ID | <b336c631-f197-47ca-bd71-48cda7f1a755@googlegroups.com> |
| In reply to | #30496 |
On Saturday, September 29, 2012 9:46:22 PM UTC+8, Ramchandra Apte wrote: > On Thursday, 27 September 2012 04:14:42 UTC+5:30, Tim Chase wrote: > > > On 09/26/12 17:28, 88888 Dihedral wrote: > > > > > > > 88888 Dihedral於 2012年9月27日星期四UTC+8上午6時07分35秒寫道: > > > > > > >>>> In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*" > > > > > > >>>> without this behavior? > > > > > > >>> >>> a = [[0]*3 for i in xrange(2)] > > > > > > >>> >>> a[0][0]=2 > > > > > > >>> >>> a > > > > > > >>> [[2, 0, 0], [0, 0, 0]] > > > > > > > > > > > > > > def zeros(m,n): > > > > > > > a=[] > > > > > > > for i in xrange(m): > > > > > > > a.append([0]*n) > > > > > > > return a > > > > > > > > > > > > > > If one wants to tranlate to C, this is the style. > > > > > > > > > > > > But this is Python, so why the heck would anybody want to emulate > > > > > > *C* style? It could also be written in an assembly-language style, > > > > > > COBOL style, or a Fortran style...none of which are particularly > > > > > > valuable. > > > > > > > > > > > > Besides, a C-style would allocate a single array of M*N slots and > > > > > > then calculate 2d offsets into that single array. :-P > > > > > > > > > > > > -tkc > > > > 88888 Dihedral is a bot. Don't you get it why I avoided the lambda one liner as a functon. I prefer the def way with a name chosen.
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2012-09-29 11:18 -0600 |
| Message-ID | <mailman.1643.1348939161.27098.python-list@python.org> |
| In reply to | #30509 |
On Sat, Sep 29, 2012 at 11:01 AM, 88888 Dihedral <dihedral88888@googlemail.com> wrote: > > Don't you get it why I avoided the lambda one liner as a functon. > > I prefer the def way with a name chosen. Certainly, but the Bresenham line algorithm is O(n), which is why it is so superior to quicksort that is O(n log n). Of course you might try the Capo Ferro optimization, but I find that Thibault cancels out Capo Ferro, don't you?
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-09-29 11:50 -0700 |
| Message-ID | <dae9ce04-57a8-4b3f-976a-c96eb6637350@googlegroups.com> |
| In reply to | #30515 |
On Sunday, September 30, 2012 1:19:22 AM UTC+8, Ian wrote: > On Sat, Sep 29, 2012 at 11:01 AM, 88888 Dihedral > > <dihedral88888@googlemail.com> wrote: > > > > > > Don't you get it why I avoided the lambda one liner as a functon. > > > > > > I prefer the def way with a name chosen. > > > > Certainly, but the Bresenham line algorithm is O(n), which is why it > > is so superior to quicksort that is O(n log n). Of course you might > > try the Capo Ferro optimization, but I find that Thibault cancels out > > Capo Ferro, don't you? OK! I'll illustrate the lazy aspect of the python interpreter furthermore. a=[1,2,3] b=[a]*4 # different from a sliced copy as [list(a)]*4 print b a[1]=4 print b #----- a=666 # type morphed print b
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-09-29 11:50 -0700 |
| Message-ID | <mailman.1652.1348944662.27098.python-list@python.org> |
| In reply to | #30515 |
On Sunday, September 30, 2012 1:19:22 AM UTC+8, Ian wrote: > On Sat, Sep 29, 2012 at 11:01 AM, 88888 Dihedral > > <dihedral88888@googlemail.com> wrote: > > > > > > Don't you get it why I avoided the lambda one liner as a functon. > > > > > > I prefer the def way with a name chosen. > > > > Certainly, but the Bresenham line algorithm is O(n), which is why it > > is so superior to quicksort that is O(n log n). Of course you might > > try the Capo Ferro optimization, but I find that Thibault cancels out > > Capo Ferro, don't you? OK! I'll illustrate the lazy aspect of the python interpreter furthermore. a=[1,2,3] b=[a]*4 # different from a sliced copy as [list(a)]*4 print b a[1]=4 print b #----- a=666 # type morphed print b
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-09-30 03:41 +1000 |
| Message-ID | <mailman.1644.1348940467.27098.python-list@python.org> |
| In reply to | #30509 |
On Sun, Sep 30, 2012 at 3:18 AM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> On Sat, Sep 29, 2012 at 11:01 AM, 88888 Dihedral
> <dihedral88888@googlemail.com> wrote:
>>
>> Don't you get it why I avoided the lambda one liner as a functon.
>>
>> I prefer the def way with a name chosen.
>
> Certainly, but the Bresenham line algorithm is O(n), which is why it
> is so superior to quicksort that is O(n log n). Of course you might
> try the Capo Ferro optimization, but I find that Thibault cancels out
> Capo Ferro, don't you?
Unless ...
class Agrippa(object):
students = []
def __init__(self):
students.append(self)
enemy = Agrippa()
ChrisA
(maybe I'm pushing this a bit too far)
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-09-29 10:01 -0700 |
| Message-ID | <mailman.1641.1348938094.27098.python-list@python.org> |
| In reply to | #30496 |
On Saturday, September 29, 2012 9:46:22 PM UTC+8, Ramchandra Apte wrote: > On Thursday, 27 September 2012 04:14:42 UTC+5:30, Tim Chase wrote: > > > On 09/26/12 17:28, 88888 Dihedral wrote: > > > > > > > 88888 Dihedral於 2012年9月27日星期四UTC+8上午6時07分35秒寫道: > > > > > > >>>> In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*" > > > > > > >>>> without this behavior? > > > > > > >>> >>> a = [[0]*3 for i in xrange(2)] > > > > > > >>> >>> a[0][0]=2 > > > > > > >>> >>> a > > > > > > >>> [[2, 0, 0], [0, 0, 0]] > > > > > > > > > > > > > > def zeros(m,n): > > > > > > > a=[] > > > > > > > for i in xrange(m): > > > > > > > a.append([0]*n) > > > > > > > return a > > > > > > > > > > > > > > If one wants to tranlate to C, this is the style. > > > > > > > > > > > > But this is Python, so why the heck would anybody want to emulate > > > > > > *C* style? It could also be written in an assembly-language style, > > > > > > COBOL style, or a Fortran style...none of which are particularly > > > > > > valuable. > > > > > > > > > > > > Besides, a C-style would allocate a single array of M*N slots and > > > > > > then calculate 2d offsets into that single array. :-P > > > > > > > > > > > > -tkc > > > > 88888 Dihedral is a bot. Don't you get it why I avoided the lambda one liner as a functon. I prefer the def way with a name chosen.
[toc] | [prev] | [next] | [standalone]
| From | Ramchandra Apte <maniandram01@gmail.com> |
|---|---|
| Date | 2012-09-29 06:46 -0700 |
| Message-ID | <mailman.1629.1348926391.27098.python-list@python.org> |
| In reply to | #30243 |
On Thursday, 27 September 2012 04:14:42 UTC+5:30, Tim Chase wrote: > On 09/26/12 17:28, 88888 Dihedral wrote: > > > 88888 Dihedral於 2012年9月27日星期四UTC+8上午6時07分35秒寫道: > > >>>> In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*" > > >>>> without this behavior? > > >>> >>> a = [[0]*3 for i in xrange(2)] > > >>> >>> a[0][0]=2 > > >>> >>> a > > >>> [[2, 0, 0], [0, 0, 0]] > > > > > > def zeros(m,n): > > > a=[] > > > for i in xrange(m): > > > a.append([0]*n) > > > return a > > > > > > If one wants to tranlate to C, this is the style. > > > > But this is Python, so why the heck would anybody want to emulate > > *C* style? It could also be written in an assembly-language style, > > COBOL style, or a Fortran style...none of which are particularly > > valuable. > > > > Besides, a C-style would allocate a single array of M*N slots and > > then calculate 2d offsets into that single array. :-P > > > > -tkc 88888 Dihedral is a bot.
[toc] | [prev] | [next] | [standalone]
| From | 88888 Dihedral <dihedral88888@googlemail.com> |
|---|---|
| Date | 2012-09-26 14:45 -0700 |
| Message-ID | <a194f613-807e-4c2a-808e-3087df6111f4@googlegroups.com> |
| In reply to | #30235 |
TP於 2012年9月27日星期四UTC+8上午5時25分04秒寫道: > Hi everybody, > > > > I have tried, naively, to do the following, so as to make lists quickly: > > > > >>> a=[0]*2 > > >>> a > > [0, 0] > > >>> a[0]=3 > > >>> a > > [3, 0] > > > > All is working fine, so I extended the technique to do: > > > > >>> a=[[0]*3]*2 > > >>> a > > [[0, 0, 0], [0, 0, 0]] > > >>> a[0][0]=2 > > >>> a > > [[2, 0, 0], [2, 0, 0]] > > > > The behavior is no more expected! > > The reason is probably that in the first case, 0 is an integer, not a list, > > so Python copies two elements that are independent. > > In the second case, the elements are [0,0,0], which is a list; when Python > > copies a list, he copies in fact the *pointer* to the list, such that we > > obtain this apparently strange behavior. > > > > Is it the correct explanation? > > In these conditions, how to make this list [[0,0,0],[0,0,0]] with "*" > > without this behavior? > > > > Thanks, > > > > TP def zeros(m,n): for i in xrange(m): for j in xrange(n): a[i][j]=0 return a >>> a=zeros(3,2) >>> a [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]] >>> I think this is what you want.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web