print(len(list_a))打印为两个,但print(list_a)打印为[]

问题描述 投票:1回答:1

我正在修改python代码,以在pygame表面上查看车道检测图像。它工作正常,但偶尔我会看到来自以下功能的错误消息(我在调试中添加了两个打印件)。

def display_lines(image, lines):
    line_image = np.zeros_like(image)
    if lines is not None:
        print('len of lines:',len(lines))
        print(lines)
        for line in lines:
            x1, y1, x2, y2 = line
            cv2.line(line_image, (x1,y1), (x2,y2), (255,0,0), 4)
    return line_image

正常时,打印如下:

len of lines: 2
[[ 251  720  998    0]
 [1026  720  281    0]]

出现错误时,错误消息如下:

len of lines: 2 
[]
Traceback (most recent call last):
  File "./automatic_control.py", line 758, in <lambda>
    self.sensor.listen(lambda image: CameraManager._parse_image(weak_self, image))
  File "./automatic_control.py", line 803, in _parse_image
    line_image = display_lines(lane_image, averaged_lines)
  File "./automatic_control.py", line 700, in display_lines
    x1, y1, x2, y2 = line
ValueError: need more than 0 values to unpack

[在错误情况下,print(lines)仅打印[],但为什么却为2打印print(len(lines))?可能是什么问题?

python numpy
1个回答
0
投票

lines不是列表。 lines是一个NumPy数组。您完全需要使用那些完全不同的类型,以了解如果要使用NumPy的区别。在使您感到困惑的情况下,lines是2x0数组。 NumPy数组的len是其第一维的长度,因此lines的长度为2,但仍包含0个元素。

我本来希望这样的数组打印为

[[] []]

但显然它打印为[]

为了获得更丰富的显示,您应该打印阵列的repr,这将显示类似的内容

array([], shape=(2, 0), dtype=something)

© www.soinside.com 2019 - 2024. All rights reserved.