为对象引用打印“[…]”是什么意思?

时间:2022-05-18 23:12:59

I'm printing a value of a what I thought was a list, but the output that I get is:

我打印了一个我认为是列表的值,但是输出是:

[...]

What does this represent? How do I test for it? I've tried:

这代表什么呢?我如何测试它?我试过了:

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

and

myVar.__repr_() != Ellipsis

but no dice...

但不行…

Here's a cutdown of the code that's giving the issue:

这里有一个问题的代码的简化:

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 a LOT of [...] supposedly recursive lists, but I can't see why. I've altered the test at #test to prevent this.

totalPaths包含许多[……]据说是递归列表,但我不明白为什么。我修改了#test的测试以防止这种情况发生。

I've 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

in order to explicitly return None for empty paths.

为了显式地为空路径返回None。

4 个解决方案

#1


32  

Depending on the context here it could different things:

根据这里的上下文,它可以是不同的东西:

indexing/slicing with Ellipsis

I think it's not implemented for any python class but it should represent an arbitary number of data structure nestings (as much needed). So for example: a[..., 1] should return all the second elements of the innermost nested structure:

我认为它不是为任何python类实现的,但是它应该代表一个任意数量的数据结构nestings(这是非常需要的)。例如:[…, 1)返回最内层嵌套结构的所有第二元素:

>>> 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 dimensionarray([[ 1,  4,  7],       [10, 13, 16],       [19, 22, 25]])>>> a[0, ...]  # This returns a slice through the first dimensionarray([[0, 1, 2],       [3, 4, 5],       [6, 7, 8]])

and to check for this ... you compare it to an Ellipsis (this is a singleton so recommended is using is:

为了检查这个。您可以将它与省略号进行比较(这是一个单例,因此建议使用:

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

Recursive Datastructures

The other case in which you see an [...] in your output is if you have the sequence inside the sequence itself. Here it stands for an infinite deeply nested sequence (that's not printable). 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 test if you have any such recursion in your output you need to check if the data-structure itself is also one of the elements:

要测试您的输出中是否有此类递归,您需要检查数据结构本身是否也是元素之一:

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

Another way to get a more meaningful output is using pprint.pprint:

另一种获得更有意义输出的方法是使用pprint.pprint:

>>> import pprint>>> pprint.pprint(alist)  # Assuming you only replaced the first element:[<Recursion on list with id=1628861250120>, 'b', 'c']

#2


48  

It represents an infinite loop within the structure. An example:

它表示结构中的无限循环。一个例子:

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

l's first item is itself. It's a recursive reference, and so python can't reasonably display its contents. Instead it shows [...]

l的第一项是它本身。它是一个递归引用,因此python不能合理地显示其内容。相反,它显示了[…]

#3


21  

If your list contains self references Python will display that as [...] rather than trying to recursively print it out, which would lead to an infinte loop:

如果您的列表包含了self references Python将会显示为[…而不是试图递归地打印出来,这将导致一个无限循环:

>>> 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': {...}}

#4


11  

It's a recursive reference as your list contains itself. Python doesn't try to recursively print this which would lead to an infinite loop.

它是一个递归引用,因为列表包含它自己。Python不会尝试递归地打印这个,这会导致无限循环。

repr detects this. So, if you looked at the internal representation of your list object you would see (where the ellipsis occur) "Reference to the same list object at address *" where * is the address of the original list object in memory. Hence, the infinite loop.

repr检测。如果你看一下列表对象的内部表示你会看到(省略号出现的地方)“在地址*处引用同一个列表对象”,其中*是内存中原始列表对象的地址。因此,无限循环。

#1


32  

Depending on the context here it could different things:

根据这里的上下文,它可以是不同的东西:

indexing/slicing with Ellipsis

I think it's not implemented for any python class but it should represent an arbitary number of data structure nestings (as much needed). So for example: a[..., 1] should return all the second elements of the innermost nested structure:

我认为它不是为任何python类实现的,但是它应该代表一个任意数量的数据结构nestings(这是非常需要的)。例如:[…, 1)返回最内层嵌套结构的所有第二元素:

>>> 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 dimensionarray([[ 1,  4,  7],       [10, 13, 16],       [19, 22, 25]])>>> a[0, ...]  # This returns a slice through the first dimensionarray([[0, 1, 2],       [3, 4, 5],       [6, 7, 8]])

and to check for this ... you compare it to an Ellipsis (this is a singleton so recommended is using is:

为了检查这个。您可以将它与省略号进行比较(这是一个单例,因此建议使用:

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

Recursive Datastructures

The other case in which you see an [...] in your output is if you have the sequence inside the sequence itself. Here it stands for an infinite deeply nested sequence (that's not printable). 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 test if you have any such recursion in your output you need to check if the data-structure itself is also one of the elements:

要测试您的输出中是否有此类递归,您需要检查数据结构本身是否也是元素之一:

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

Another way to get a more meaningful output is using pprint.pprint:

另一种获得更有意义输出的方法是使用pprint.pprint:

>>> import pprint>>> pprint.pprint(alist)  # Assuming you only replaced the first element:[<Recursion on list with id=1628861250120>, 'b', 'c']

#2


48  

It represents an infinite loop within the structure. An example:

它表示结构中的无限循环。一个例子:

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

l's first item is itself. It's a recursive reference, and so python can't reasonably display its contents. Instead it shows [...]

l的第一项是它本身。它是一个递归引用,因此python不能合理地显示其内容。相反,它显示了[…]

#3


21  

If your list contains self references Python will display that as [...] rather than trying to recursively print it out, which would lead to an infinte loop:

如果您的列表包含了self references Python将会显示为[…而不是试图递归地打印出来,这将导致一个无限循环:

>>> 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': {...}}

#4


11  

It's a recursive reference as your list contains itself. Python doesn't try to recursively print this which would lead to an infinite loop.

它是一个递归引用,因为列表包含它自己。Python不会尝试递归地打印这个,这会导致无限循环。

repr detects this. So, if you looked at the internal representation of your list object you would see (where the ellipsis occur) "Reference to the same list object at address *" where * is the address of the original list object in memory. Hence, the infinite loop.

repr检测。如果你看一下列表对象的内部表示你会看到(省略号出现的地方)“在地址*处引用同一个列表对象”,其中*是内存中原始列表对象的地址。因此,无限循环。