【发布时间】:2019-03-06 18:14:43
【问题描述】:
我正在尝试将值从 0 到 15 的数组重新分类为从 0 到 5 的新值。
我的条件如下:
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))))))
选项 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)
)))))
【问题讨论】:
-
con1等是什么? -
这似乎是一个代码审查问题:)
-
使用循环,卢克