What does Python mean by typing "[...]" to refer to an object?

I am printing the meaning of what I thought was a list, but the output I get is:

[...] 

What does this mean? How can I check this? I tried:

 myVar.__repr__() != '[...]' 

and

 myVar.__repr_() != Ellipsis 

but not bones ...

Here's cutting out the code that gives the problem:

 def buildPaths(graph, start, end, path=[], totalPaths=[]): """ returns list of all possible paths from start node to the end node """ path = path + [start] if start == end: return path for nextNode in graph.childrenOf(start): if nextNode not in path: newPath = buildPaths(graph, nextNode, end, path, totalPaths) if newPath != []: # test totalPaths.append(newPath) return totalPaths 

totalPaths contains many [...] presumably recursive lists, but I don’t understand why. I changed the test to #test to prevent this.

I also tried:

 def buildPaths(graph, thisNode, end, path=[], totalPaths=None): """ returns list of all possible paths from start node to the end node """ path = path + [thisNode] if thisNode == end: return path for nextNode in graph.childrenOf(thisNode): if nextNode not in path: newPath = buildPaths(graph, nextNode, end, path, totalPaths) if newPath != None: if totalPaths == None: totalPaths = [newPath] else: totalPaths.append(newPath) return totalPaths 

to explicitly return None for empty paths.

+49
python recursion ellipsis recursive-datastructures
Apr 28 '16 at 2:46
source share
4 answers

There may be different things, depending on the context:

indexing / slicing with Ellipsis

I think it is not implemented for any python class, but it should be an arbitrary number of data structure attachments (as needed). So, for example: a[..., 1] should return all other elements of the innermost nested structure:

 >>> import numpy as np >>> a = np.arange(27).reshape(3,3,3) # 3dimensional array >>> a[..., 1] # this returns a slice through the array in the third dimension array([[ 1, 4, 7], [10, 13, 16], [19, 22, 25]]) >>> a[0, ...] # This returns a slice through the first dimension array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) 

and to verify this ... you are comparing it to Ellipsis (this is a singleton, so it is recommended to use is :

 >>> ... is Ellipsis True >>> Ellipsis in [...] True # Another (more or less) equivalent alternative to the previous line: >>> any(i is Ellipsis for i in [1, ..., 2]) True 

Recursive Data Structures

Another case when you see [...] in your conclusion is if you have a sequence within the sequence itself. Here it denotes an infinite deeply nested sequence (which is not printed). For example:

 >>> alist = ['a', 'b', 'c'] >>> alist[0] = alist >>> alist [[...], 'b', 'c'] # Infinite deeply nested so you can use as many leading [0] as you want >>> alist[0][1] 'b' >>> alist[0][0][0][0][0][1] 'b' >>> alist[0][0][0][0][0][0][0][0][0][0][0][0][0][0][0][1] 'b' 

You can even replace it several times:

 >>> alist[2] = alist >>> alist [[...], 'b', [...]] >>> alist[1] = alist >>> alist [[...], [...], [...]] 

To check if you have such recursion in your output, you need to check if the data structure itself is also one of the elements:

 >>> alist in alist True >>> any(i is alist for i in alist) True 

Another way to get a more meaningful result is to use pprint.pprint :

 >>> import pprint >>> pprint.pprint(alist) # Assuming you only replaced the first element: [<Recursion on list with id=1628861250120>, 'b', 'c'] 
+32
Apr 28 '16 at 3:03
source share

It is an endless cycle within a structure. Example:

 In [1]: l = [1, 2] In [2]: l[0] = l In [3]: l Out[3]: [[...], 2] 

l first element in itself. This is a recursive link, and therefore python cannot intelligently display its contents. Instead, he shows [...]

+48
Apr 28 '16 at 2:48
source share

If your list contains its own links, Python will display this as [...] rather than trying to print it recursively, which will result in an infinte loop:

 >>> l = [1, 2, 3] >>> print(l) [1, 2, 3] >>> l.append(l) >>> print(l) [1, 2, 3, [...]] >>> print(l[-1]) # print the last item of list l [1, 2, 3, [...]] >>> print(l[-1][-1]) # print the last item of the last item of list l [1, 2, 3, [...]] 

ad infinitum.

A similar situation arises with dictionaries:

 >>> d = {} >>> d['key'] = d >>> print(d) {'key': {...}} >>> d['key'] {'key': {...}} >>> d['key']['key'] {'key': {...}} 
+21
Apr 28 '16 at 2:50
source share

This is a recursive link since your list contains itself. Python does not try to print this recursively, resulting in an infinite loop.

repr discovers this. So, if you looked at the internal representation of your list object, you will see (where the ellipsis occurs) "Link to the same list object at the address *", where * is the address of the original list object in memory. Consequently, an infinite loop.

+11
Apr 28 '16 at 2:49 on
source share



All Articles