Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 分裂这条线的最像蟒蛇的方式?_Python_Numpy_Pep8_Pep - Fatal编程技术网

Python 分裂这条线的最像蟒蛇的方式?

Python 分裂这条线的最像蟒蛇的方式?,python,numpy,pep8,pep,Python,Numpy,Pep8,Pep,我正在尝试用0到5的新值对值从0到15的数组进行重新分类 我的情况如下: con1 = np.in1d(arr, [0, 11, 13, 15]).reshape((y, x)) # new val 0 con2 = np.in1d(arr, [1, 2, 3, 4, 5]).reshape((y, x)) # new val 1 con3 = (arr == 6) | (arr == 7) # new val 2 con4 = (arr ==

我正在尝试用0到5的新值对值从0到15的数组进行重新分类

我的情况如下:

con1 = np.in1d(arr, [0, 11, 13, 15]).reshape((y, x))  # new val 0
con2 = np.in1d(arr, [1, 2, 3, 4, 5]).reshape((y, x))  # new val 1
con3 = (arr == 6) | (arr == 7)                        # new val 2
con4 = (arr == 8) | (arr == 9)                        # new val 3
con5 = (arr == 10)                                    # new val 4
con6 = (arr == 12) | (arr == 14)                      # new val 5
我有下面一行python代码

return np.where(con1, 0, np.where(con2, 1, np.where(con3, 2, np.where(con4, 3, np.where(con5, 4, np.where(con6, 5, arr))))))
长度为128个字符(包括函数内部的缩进)。PEP8建议行不应超过79个字符。但是,我不确定在保持可读性的同时,将这一行拆分为多行的最佳方法是什么

我尝试了两种选择,但它们似乎很难阅读

选项1:

return np.where(con1, 0, np.where(
    con2, 1, np.where(
        con3, 2, np.where(
            con4, 3, np.where(
                con5, 4, np.where(
                    con6, 5, arr))))))
return np.where(con1, 0, 
                np.where(con2, 1, 
                         np.where(con3, 2, 
                                  np.where(con4, 3, 
                                           np.where(con5, 4, 
                                                    np.where(con6, 5, arr)
                                                    )))))
选项2:

return np.where(con1, 0, np.where(
    con2, 1, np.where(
        con3, 2, np.where(
            con4, 3, np.where(
                con5, 4, np.where(
                    con6, 5, arr))))))
return np.where(con1, 0, 
                np.where(con2, 1, 
                         np.where(con3, 2, 
                                  np.where(con4, 3, 
                                           np.where(con5, 4, 
                                                    np.where(con6, 5, arr)
                                                    )))))

你可以分别做。这更具可读性,因为您可以一步一步地进行操作

filtered_result = np.where(con6, 5, arr)
filtered_result = np.where(con5, 4, filtered_result)
filtered_result = np.where(con4, 3, filtered_result)
filtered_result = np.where(con3, 2, filtered_result)
filtered_result = np.where(con2, 1, filtered_result)
filtered_result = np.where(con1, 0, filtered_result)

return filtered_result
要坚持pep8,这就是你所要求的,那么这就是前进的方向

编辑

for循环还将显著减少重复性,并且仍然可读

connections = iter((con6, con5, con4, con3, co2, con1, con0))
filters = range(len(connections)-2, 0 -1)

filtered_result = np.where(next(connections), next(filters), arr)

for n, con, in zip(filters, connections):
    filtered_result = np.where(con, n, filtered_result)

return filtered_result

你可以分别做。这更具可读性,因为您可以一步一步地进行操作

filtered_result = np.where(con6, 5, arr)
filtered_result = np.where(con5, 4, filtered_result)
filtered_result = np.where(con4, 3, filtered_result)
filtered_result = np.where(con3, 2, filtered_result)
filtered_result = np.where(con2, 1, filtered_result)
filtered_result = np.where(con1, 0, filtered_result)

return filtered_result
要坚持pep8,这就是你所要求的,那么这就是前进的方向

编辑

for循环还将显著减少重复性,并且仍然可读

connections = iter((con6, con5, con4, con3, co2, con1, con0))
filters = range(len(connections)-2, 0 -1)

filtered_result = np.where(next(connections), next(filters), arr)

for n, con, in zip(filters, connections):
    filtered_result = np.where(con, n, filtered_result)

return filtered_result

可能不太容易阅读,但您可以尝试
reduce

from functools import reduce

def some_func():

    ... some code maybe ...

    args = [con1, con2, con3, con4, con5, con6]
    return reduce(
            lambda prev_arg, new_arg: np.where(*new_arg, prev_arg), 
            enumerate(args[:-1]), 
            np.where(args[-1], len(args)-1, arr)
        )

可能不太容易阅读,但您可以尝试
reduce

from functools import reduce

def some_func():

    ... some code maybe ...

    args = [con1, con2, con3, con4, con5, con6]
    return reduce(
            lambda prev_arg, new_arg: np.where(*new_arg, prev_arg), 
            enumerate(args[:-1]), 
            np.where(args[-1], len(args)-1, arr)
        )

什么是
con1
等等?这似乎是一个代码复查问题:)使用循环,lukey什么是
con1
等等?这似乎是一个代码复查问题:)使用循环,LukeYou甚至可能想在
枚举([con6,con5,…])上使用
for
循环
我会提醒大家,如果我分开调用,我会遇到重叠值的问题,但事实并非如此。循环是个好主意,谢谢你的回答!您甚至可能想使用
for
循环,例如
枚举([con6,con5,…])
我会提醒您,如果我分隔调用,我会遇到重叠值的问题,但事实并非如此。循环是个好主意,谢谢你的回答!