Re: a problem about "print"

Discussion in 'Python' started by Mark Lawrence, Jul 4, 2012.

  1. @Matteo, @Levi, please don't top post it makes following a thread very
    difficult, no other comments, TIA.

    On 04/07/2012 08:51, Matteo Boscolo wrote:
    > in the code2
    >
    > aList=[1,2,3,4,5,6,7,8,9,10]
    > aList=str(aList) #<--- here you convert the list in a string
    >
    > print aList
    > print aList[2] #<-- here you are printing the third caracter of the
    > string '[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]' not the list '[1, 2, 3, 4, 5,
    > 6, 7, 8, 9, 10]'
    >
    > regards
    > Matteo
    >
    >
    >
    > Il 04/07/2012 09:28, levi nie ha scritto:
    >> Hi,Harrison.
    >> Your method is cool.
    >> But i doubt this, if bList and aList just are attached to the same
    >> List when i write bList=aList,but why the output of the following two
    >> code are different?
    >>
    >> code1:
    >> aList=[1,2,3,4,5,6,7,8,9,10]
    >> bList=aList
    >> bList=str(bList)
    >> print aList
    >> print aList[2]
    >>
    >> code2:
    >> aList=[1,2,3,4,5,6,7,8,9,10]
    >> aList=str(aList)
    >> print aList
    >> print aList[2]
    >>
    >> i'm puzzled now.
    >>
    >> 2012/7/4 Harrison Morgan <
    >> <mailto:>>
    >>
    >>
    >>
    >> On Wed, Jul 4, 2012 at 12:38 AM, levi nie <
    >> <mailto:>> wrote:
    >>
    >> that's good,thanks.
    >> new problem.
    >> when i write
    >> bList=aList
    >> del bList[2]
    >> bList and aList both change,how can i make aList not changed?
    >>
    >>
    >>
    >> Lists are mutable. That means that when you do bList = aList,
    >> you're just creating another reference to aList. They both point
    >> to the same list, just using different names. You should read up a
    >> bit on immutable vs. mutable objects. Here's something that I
    >> found that might explain it a bit better.
    >> http://henry.precheur.org/python/copy_list
    >>
    >>
    >>
    >>

    >
    >
    >
    >
    >



    --
    Cheers.

    Mark Lawrence.
     
    Mark Lawrence, Jul 4, 2012
    #1
    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. Bad_Kid
    Replies:
    3
    Views:
    3,956
    Shawn H. Mesiatowsky
    Apr 7, 2005
  2. Bryan Shaw

    Lock Print Screen and File Print

    Bryan Shaw, Apr 19, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,958
    Kevin Spencer
    Apr 20, 2005
  3. keto
    Replies:
    0
    Views:
    980
  4. Lew
    Replies:
    0
    Views:
    634
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    366
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page