首页 > 解决方案 > 检查列表中的所有元素是否都是列表类型并且长度是否大于零

问题描述

我编写了一个类,其中输入变量是带有字符串的列表列表。

每个子列表至少应包含一个字符串。

要检查主列表中的所有元素是否都是列表以及输入长度是否正确,我有以下代码:

for i, doc in enumerate(self.input_file_):
            assert isinstance(doc,list),'The element of input_file at index ' + str(i) + ' is not a list'
            assert len(doc) > 0, 'The input_file has an empty list at index ' + str(i) + ' and should contain at least one str value'

有没有比遍历列表更快/更优雅的方法来做到这一点?

标签: pythonloopsassertion

解决方案


推荐阅读