a problem about "print"

Matteo Boscolo matteo.boscolo at boscolini.eu
Wed Jul 4 03:51:17 EDT 2012


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 <harrison.morgan at gmail.com 
> <mailto:harrison.morgan at gmail.com>>
>
>
>
>     On Wed, Jul 4, 2012 at 12:38 AM, levi nie <levinie001 at gmail.com
>     <mailto:levinie001 at gmail.com>> 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
>
>
>
>


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120704/022b2722/attachment.html>


More information about the Python-list mailing list