using "*" to make a list of lists with repeated (and independent) elements

Discussion in 'Python' started by TP, Sep 26, 2012.

  1. TP

    TP Guest

    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
     
    TP, Sep 26, 2012
    #1
    1. Advertising

  2. TP

    Ian Kelly Guest

    Re: using "*" to make a list of lists with repeated (and independent)elements

    On Wed, Sep 26, 2012 at 3:20 PM, TP <> 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.
     
    Ian Kelly, Sep 26, 2012
    #2
    1. Advertising

  3. TP

    Paul Rubin Guest

    TP <> 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]]
     
    Paul Rubin, Sep 26, 2012
    #3
  4. 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[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.
     
    88888 Dihedral, Sep 26, 2012
    #4
  5. Paul Rubinæ–¼ 2012å¹´9月27日星期四UTC+8上åˆ5時43分58秒寫é“:
    > TP <> 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.
     
    88888 Dihedral, Sep 26, 2012
    #5
  6. 88888 Dihedralæ–¼ 2012å¹´9月27日星期四UTC+8上åˆ6時07分35秒寫é“:
    > Paul Rubinæ–¼ 2012å¹´9月27日星期四UTC+8上åˆ5時43分58秒寫é“:
    >
    > > TP <> 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.
     
    88888 Dihedral, Sep 26, 2012
    #6
  7. TP

    Tim Chase Guest

    Re: using "*" to make a list of lists with repeated (and independent)elements

    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
     
    Tim Chase, Sep 26, 2012
    #7
  8. 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.
     
    88888 Dihedral, Sep 26, 2012
    #8
  9. Re: using "*" to make a list of lists with repeated (and independent)elements

    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.
     
    88888 Dihedral, Sep 26, 2012
    #9
  10. 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.
     
    88888 Dihedral, Sep 27, 2012
    #10
  11. Re: using "*" to make a list of lists with repeated (and independent)elements

    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.
     
    88888 Dihedral, Sep 27, 2012
    #11
  12. 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.
     
    Ramchandra Apte, Sep 29, 2012
    #12
  13. Re: using "*" to make a list of lists with repeated (and independent)elements

    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.
     
    Ramchandra Apte, Sep 29, 2012
    #13
  14. 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.
     
    88888 Dihedral, Sep 29, 2012
    #14
  15. Re: using "*" to make a list of lists with repeated (and independent)elements

    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.
     
    88888 Dihedral, Sep 29, 2012
    #15
  16. TP

    Ian Kelly Guest

    Re: using "*" to make a list of lists with repeated (and independent)elements

    On Sat, Sep 29, 2012 at 11:01 AM, 88888 Dihedral
    <> 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?
     
    Ian Kelly, Sep 29, 2012
    #16
  17. Re: using "*" to make a list of lists with repeated (and independent)elements

    On Sun, Sep 30, 2012 at 3:18 AM, Ian Kelly <> wrote:
    > On Sat, Sep 29, 2012 at 11:01 AM, 88888 Dihedral
    > <> 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)
     
    Chris Angelico, Sep 29, 2012
    #17
  18. On Sunday, September 30, 2012 1:19:22 AM UTC+8, Ian wrote:
    > On Sat, Sep 29, 2012 at 11:01 AM, 88888 Dihedral
    >
    > <> 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
     
    88888 Dihedral, Sep 29, 2012
    #18
  19. Re: using "*" to make a list of lists with repeated (and independent)elements

    On Sunday, September 30, 2012 1:19:22 AM UTC+8, Ian wrote:
    > On Sat, Sep 29, 2012 at 11:01 AM, 88888 Dihedral
    >
    > <> 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
     
    88888 Dihedral, Sep 29, 2012
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wolfgang Lipp
    Replies:
    0
    Views:
    378
    Wolfgang Lipp
    Feb 9, 2004
  2. cppaddict
    Replies:
    3
    Views:
    318
    AciD_X
    Jun 22, 2004
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    410
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. antar2
    Replies:
    2
    Views:
    393
    Bighead
    Jul 17, 2008
  5. antar2
    Replies:
    3
    Views:
    370
    Niklas Norrthon
    Jul 23, 2008
Loading...

Share This Page