python - 检查列表中的所有元素是否都是列表类型并且长度是否大于零
问题描述
我编写了一个类,其中输入变量是带有字符串的列表列表。
每个子列表至少应包含一个字符串。
要检查主列表中的所有元素是否都是列表以及输入长度是否正确,我有以下代码:
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'
有没有比遍历列表更快/更优雅的方法来做到这一点?
解决方案
推荐阅读
- python - 具有多个根元素的“XML”文档
- python - 将 .txt 文件中的元素作为列表而不是字符串打开
- python - 如何使用 Django 中的选择 - 如何显示特定内容?
- android - 该位置没有 XML:http://schemas.android.com/apk/res/android
- ruby-on-rails - 如何从静态站点向 Rails 应用程序发布请求?
- javascript - firebase.fireStore() 不是函数
- c# - 使用音频剪辑获取 1 秒的样本?
- mysql - 如何从 MySQL 中删除自定义帖子类型?
- bash - sh 脚本捕获文本并添加到空格分隔的变量
- php - 使用为移动应用程序构建的 REST API 创建 Web 前端