Loops 理解列表中循环的双变量嵌套
我试图通过实现理解列表来改进代码 我有以下声明Loops 理解列表中循环的双变量嵌套,loops,nested,list-comprehension,nested-loops,Loops,Nested,List Comprehension,Nested 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中的列表(列作为键)和
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变量,该循环仅在使用变量本身之后执行
请记住,将理解列为一个反向结构是正确的,b在项
定义之前,然后是循环
定义,但在每个部分中,逻辑顺序与正常循环代码相同