loops - 理解列表中的双变量嵌套循环
问题描述
我正在尝试通过实现理解列表来改进我的代码。
我有以下声明。
test = [expression.match(self.sourceModel().index(source_row, column, source_parent).data())
for expression in liste for liste, column in self._filters.items()]
测试是字符串之间的正则表达式匹配,来自表达式列表,包含在 dict 中的列表,列作为键,表达式列表作为值
我不明白为什么我的变量“liste”被标记为未引用,因为它在最后一个 for 循环中被提及,有什么想法吗?
解决方案
代码
test = [expression.match(self.sourceModel().index(source_row, column, source_parent).data()) for expression in liste for liste, column in self._filters.items() for expression in liste ]
解释
您的liste
变量未被引用,因为您在循环中引用了 liste 变量,该循环仅在您使用 var 本身后执行!
item
请记住,在定义之前,然后是定义,认为列表理解是一个颠倒的结构是正确的loop
,但是在每个部分中,逻辑顺序与正常循环代码相同
推荐阅读
- android - 尝试使用 Kotlin 创建 pdf 文件。获取“java.io.FileNotFoundException:20210922:打开失败:EROFS(只读文件系统)”错误/
- mysql - 从链接表中获取 MIN、MAX 值
- reactjs - 我正在开发一个使用导入 Map 而不是 MapContainer 的 react-leaflet 版本的项目
- php - 通过嵌套数组PHP的单个键合并单个多维数组
- entity-framework-core - EF 核心中没有鉴别器的每个层次结构的表
- node.js - 使用 socket.io 时未添加消息
- java - 我无法从 ArrayList 获取随机元素
- laravel - 如何从 API 资源中的另一个关系中检索数据透视表
- java - Gson Streaming API:无法读取 JsonObject 的所有迭代
- r - 在 R Shiny 中使用 renderTable 时如何格式化数字行输出?