] 
Author Message
 ]

# Hi.
#
# I tried following:

Quote:
>>> a = []
>>> a.append(a)
>>> a

[[...]]

# What does it mean?
# Then I tried:

Quote:
>>> a[0].append(a)

[[...], [...]]

# And now I understand completely nothing
#
# Michal Bozom

#



Tue, 03 Sep 2002 03:00:00 GMT  
 ]

Quote:
> >>> a = []
> >>> a.append(a)
> >>> a
> [[...]]

> # What does it mean?

Since 'a' contains a reference to 'a', which contains a reference to 'a',
et cetera, the python __repr__ string would be infinite, so '...' is used
to indicate this fact.

Quote:
> # Then I tried:

> >>> a[0].append(a)
> [[...], [...]]

> # And now I understand completely nothing

Same deal, except 'a[0]' which *is* 'a' contains 2 references to 'a', etc.

So you can do:

Quote:
>>> a[0][0][0][0][0][0][0][0][0][0][0][0][0][0].....

[[...]]

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



Tue, 03 Sep 2002 03:00:00 GMT  
 ]
[posted & mailed]

On Mar 17, Michal Bozon said:

Quote:
>>>> a = []
>>>> a.append(a)
>>>> a
>[[...]]

># What does it mean?

It's Python's way of saying you have a circular data structure.

Quote:
>>> foo = [ [1,2,3] ]
>>> foo
[[1, 2, 3]]
>>> foo[0][1] = foo[0]
>>> foo

[[1, [...], 3]]

--

http://www.pobox.com/~japhy/                  http://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/



Tue, 03 Sep 2002 03:00:00 GMT  
 ]

Quote:
> # Hi.
> #
> # I tried following:

> >>> a = []
> >>> a.append(a)
> >>> a
> [[...]]

> # What does it mean?

It means that you added a as the last element of a and created a recursive
structure. The "..." stands for infinite recursion.

--

  Magnus
  Lie



Tue, 03 Sep 2002 03:00:00 GMT  
 ]


Quote:
> # Hi.
> #
> # I tried following:

> >>> a = []
> >>> a.append(a)
> >>> a
> [[...]]

> # What does it mean?

a = [a] = [[a]] = [[[a]]] = [[[[a]]]]]
a is a list whose only element is a list whose only element is a list whose only element is...

You can do a trick with slicing to do what I think you expect.

Quote:
>>> a=[]
>>> a.append(a[:])
>>> a

[[]]


Tue, 03 Sep 2002 03:00:00 GMT  
 ]
On Fri, 17 Mar 2000 14:14:57 GMT, the infinitely wise Jeff Pinyan

Quote:
>>[[...]]
>It's Python's way of saying you have a circular data structure.

Interesting. Is there a programmatic way to check for this? I didn't
see any mention of it in the FAQ. If I do:

Quote:
>>> a = []
>>> a.append(a)
>>> type(a[0])

<type 'list'>

All you get is that its a list, nothing else. It would be nice to have
some more bulletproof way than the only other approach I can think of:

Quote:
>>> def IsRecursiveList(x):
>>>     return str(x) == '[[...]]'

Any ideas?

--
=======================
Alan Daniels




Wed, 04 Sep 2002 03:00:00 GMT  
 ]

Quote:
> It would be nice to have some more bulletproof way than the only other
> approach I can think of:

See Tim Peters' plumbo.py for something more complete along these lines:

from types import ListType

def IsRecursiveList(x):
    element_ids = {}
    element_ids[id (x)] = None
    for element in x:
        if type (element) == ListType:
            element_id = id (element)
            if element_ids.has_key (element_id):
                return 1
            else:
                element_ids[element_id] = None
    else:
        return 0

if __name__ == '__main__':
    a = []
    a.append (a)
    assert IsRecursiveList (a)
    assert not IsRecursiveList ([])            

Alex.



Wed, 04 Sep 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 
 

 
Powered by phpBB® Forum Software