strange append

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • E.Nurminski

    #1

    strange append


    Hello to all good people

    I am new to the great Py so am quite puzzled by the following code

    ---------------

    res = []
    x = [ 1, 1 ]
    for i in xrange(0,5):
    res.append(x)
    x[1] = x[1] + 1
    print "x = ", x
    print "res = ", res

    ---------------

    Looks like it puts smth like reference to 'x' into 'res' list, instead of
    value. But if I want a value should I use a different method or what ?

    Evgeni

    P.S. Could not easily find the issue in the manual/tutorial can you point
    me out to smth relevant ?

  • James Stroud

    #2
    Re: strange append

    E.Nurminski wrote:
    Hello to all good people
    >
    I am new to the great Py so am quite puzzled by the following code
    >
    ---------------
    >
    res = []
    x = [ 1, 1 ]
    for i in xrange(0,5):
    res.append(x)
    x[1] = x[1] + 1
    print "x = ", x
    print "res = ", res
    >
    ---------------
    >
    Looks like it puts smth like reference to 'x' into 'res' list, instead of
    value. But if I want a value should I use a different method or what ?
    >
    Evgeni
    >
    P.S. Could not easily find the issue in the manual/tutorial can you point
    me out to smth relevant ?
    >
    Yes the same reference gets added every time.

    res = []
    x = [1, 1]
    for i in xrange(0,5):
    newx = x[:] # copy the x
    res.append(newx ) # append the copy
    newx[1] += 1 # shorthand
    print "newx = %s" % newx # basic formatting
    print "res = %s" % res # should be what you expect

    James

    --
    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095


    Comment

    • Bruno Desthuilliers

      #3
      Re: strange append

      E.Nurminski wrote:
      Hello to all good people
      >
      I am new to the great Py so am quite puzzled by the following code
      >
      ---------------
      >
      res = []
      x = [ 1, 1 ]
      for i in xrange(0,5):
      res.append(x)
      x[1] = x[1] + 1
      print "x = ", x
      print "res = ", res
      >
      ---------------
      >
      Looks like it puts smth like reference to 'x' into 'res' list, instead of
      value. But if I want a value should I use a different method or what ?
      What difference do you make between "values" and "references " ?-)

      Hint 1: in Python, all you have are objects. Yes, even strings and
      numbers etc...
      Evgeni
      >
      P.S. Could not easily find the issue in the manual/tutorial can you point
      me out to smth relevant ?
      >
      Hint 2 : Python has something very nice which is the interactive python
      shell. This lets you try code snippets and see by yourself how it really
      works :

      bruno@bousin ~ $ python
      Python 2.4.3 (#1, Sep 29 2006, 20:26:46)
      [GCC 4.1.1 (Gentoo 4.1.1-r1)] on linux2
      Type "help", "copyright" , "credits" or "license" for more information.
      >>mylist = [1, "aaa"]
      >>mylist.append (42)
      >>mylist.append ("lala")
      >>mylist
      [1, 'aaa', 42, 'lala']
      >>>
      HTH
      --
      bruno desthuilliers
      python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
      p in 'onurb@xiludom. gro'.split('@')])"

      Comment

      • James Stroud

        #4
        Re: strange append

        E.Nurminski wrote (off the list):
        the intention was to have
        >
        newx = [1, 2]
        res = [[1, 2]]
        newx = [1, 3]
        res = [[1, 2], [1, 3]]
        newx = [1, 4]
        res = [[1, 2], [1, 3], [1, 4]]
        newx = [1, 5]
        res = [[1, 2], [1, 3], [1, 4], [1, 5]]
        newx = [1, 6]
        res = [[1, 2], [1, 3], [1, 4], [1, 5], [1, 6]]
        This shows how valuable it is to post your expected results with your code.

        The way most experienced python programmers might do this is with list
        comprehension:

        res = [[1, i] for i in xrange(1,7)]

        For newer programmers, this might make more sense:

        res = []
        for i in xrange(1,7):
        res.append([1,i])

        James

        --
        James Stroud
        UCLA-DOE Institute for Genomics and Proteomics
        Box 951570
        Los Angeles, CA 90095


        Comment

        Working...