Re: root[:]=[root,root]
On 12/16/2011 9:40 PM, YAN HUA wrote: Hi,all. Could anybody tell how this code works? >>> root = [None, None] >>> root[:] = [root, root] >>> root [[...], [...]] >>> root[0] [[...], [...]] >>> root[0][0][1][1][0][0][0][1][1] [[...], [...]] A simpler example: >>> l = [] >>> l.append(l) >>> l [[...]] Python is (now) smart enough to recognize a recursive list and print '...' instead of going into an infinite loop printing '['s (as it once did, I believe). -- Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list
Re: root[:]=[root,root]
On 12/17/2011 01:40 PM, YAN HUA wrote: Hi,all. Could anybody tell how this code works? >>> root = [None, None] First, you're creating a list of two None, let's say it's list-1. Then you bind the name 'root' to list-1. >>> root[:] = [root, root] Next, you assign list-1's first member with list-1 and list-1's second member with list-1. >>> root [[...], [...]] The result is a recursive list, both list-1's first and second member is list-1 itself. >>> root[0] [[...], [...]] >>> root[0][0][1][1][0][0][0][1][1] [[...], [...]] >>> Thanks. -- http://mail.python.org/mailman/listinfo/python-list
root[:]=[root,root]
Hi,all. Could anybody tell how this code works? >>> root = [None, None] >>> root[:] = [root, root] >>> root [[...], [...]] >>> root[0] [[...], [...]] >>> root[0][0][1][1][0][0][0][1][1] [[...], [...]] >>> Thanks. -- http://mail.python.org/mailman/listinfo/python-list