在Python中使用OpenCV的轮廓:如何解决“list index out of range”问题

25 浏览
0 Comments

在Python中使用OpenCV的轮廓:如何解决“list index out of range”问题

例如,如果传递了以下内容:

a = []

我该如何检查 a 是否为空?

admin 更改状态以发布 2023年5月22日
0
0 Comments

Pythonic的方法是从PEP 8风格指南中获得的。

对于序列(字符串、列表、元组),使用空序列为假的特性:

# Correct:
if not seq:
if seq:
# Wrong:
if len(seq):
if not len(seq):

0
0 Comments

if not a:
    print("List is empty")

使用空的list的隐式布尔值非常具有Pythonic风格。(参见官方文档)

0