2

概括:

在 Python 项目中,我需要对具有相同内部索引和相邻外部索引的列表列表的每两个元素应用一个函数。输出存储在一个新矩阵中。

我编写的代码有效,但并不优雅,pyflakes 抱怨它。

如何清理此代码?

附加信息:

我正在编写的代码是解决数字难题的模块的一部分。

有一次,我正在遍历一个类实例列表。
它们代表运动场中的行中的单元格。
我需要对每两个垂直相邻的单元格应用一个函数,
并将其输出存储在一个新矩阵中。

在这里,一对中的哪个单元格首先无关紧要,但这些对需要按顺序排列。

代码摘录:

def func(cell_matrix):
    out_matrix = []
    for y_pos, line in enumerate(cell_matrix):
        out_line = []
        if y_pos != 0:
            for x_pos, cell in enumerate(line):
                out_line.append(compare_func(prev_line[x_pos], cell)
            out_matrix.append(out_line)
        prev_line = line
    return out_matrix

pyflakes 抱怨什么:

Line 7: pyflakes [E]: undefined name 'prev_line'
Line 9: pyflakes [E]: local variable 'prev_line' is assigned to but never used
4

4 回答 4

1

您需要在使用它之前声明变量名称:

def func(cell_matrix):
    out_matrix = []
    prev_line = cell_matrix[0]    # use 1st line as prev_line
    for line in cell_matrix[1:]:  # use 2nd to nth line, no y_pos used
                                  # in the following code so no need to enumerate
        out_line = []
            for x_pos, cell in enumerate(line):
                out_line.append(compare_func(prev_line[x_pos], cell) ) # missing )
            out_matrix.append(out_line)
        prev_line = line
    return out_matrix
于 2019-06-07T15:39:48.440 回答
1

我建议只使用索引,这样你就不用 prev_ 变量了。

例如

def func(cell_matrix):                                                           
    out_matrix = []                                                              
    for y_pos in range(len(cell_matrix)):                                        
        out_line = []                                                            
        if y_pos != 0:                                                           
            for x_pos in range(len(cell_matrix[y_pos])):                         
                out_line.append(compare_func(cell_matrix[y_pos-1][x_pos],        
                                             cell_matrix[y_pos][x_pos]))         
            out_matrix.append(out_line)                                          
    return out_matrix                                                            

但是通过使用推导可以更加简化:

def func(cell_matrix):                                                           
    return [[compare_func(                                                       
                 cell_matrix[y_pos-1][x_pos], cell_matrix[y_pos][x_pos])         
             for x_pos in range(len(cell_matrix[y_pos]))]                        
            for y_pos in range(1, len(cell_matrix))]                             

编辑:顺便说一句,你得到的错误是 pyflakes 消息,代码运行良好 afaik。(有人可能会说这是 pyflakes 无法正确解析代码)

于 2019-06-07T15:54:32.400 回答
0

将您的代码更改为:

def func(cell_matrix):
    out_matrix = []
    prev_line = []
    for y_pos, line in enumerate(cell_matrix):
        out_line = []
        if y_pos != 0:
            for x_pos, cell in enumerate(line):
                out_line.append(compare_func(prev_line[x_pos], cell)
            out_matrix.append(out_line)
        prev_line = line
    return out_matrix

您需要prev_line在 for 循环范围之外声明,以便它可以在每个循环中使用它。

于 2019-06-07T15:37:36.713 回答
0

我最终将迭代移动到一个单独的函数,因为我发现这样更容易阅读。

def subsequences(var, r=2):
    """Yield subsequences of var with length r"""
    # subsequences("ABCD") --> AB BC CD
    for index in range(len(var)-r+1):
        yield tuple(var[index+n] for n in range(r))

def func(cell_matrix):
    return [
        [
            compare_func(cell_a, cell_b)
            for cell_a, cell_b in zip(prev_line, line)
        ]
        for prev_line, line in subsequences(cell_matrix)
    ]

不过可能只是个人口味。

于 2019-06-07T19:39:16.287 回答