gpt4 book ai didi

Pythonic 重构方式更长 0​​x104567910 if-condition-assignment (三元 If )

转载 作者:行者123 更新时间:2023-12-01 01:10:06 25 4
gpt4 key购买 nike

我当前的代码使用终身分配 One line if-condition-assignment ,但使用更详细的标识符,很容易超过行长度限制。

由于我还没有进入Pythonic编码,我很高兴看到一些重构建议。

for label in range(num_labels):
d_tresh = drop_treshold[label] if type(drop_treshold) == numpy.ndarray) else drop_treshold
r_tresh1 = relabel_treshold1[label] if type(relabel_treshold1) == numpy.ndarray else relabel_treshold1
r_tresh2 = relabel_treshold2[label] if type(relabel_treshold2) == numpy.ndarray else relabel_treshold2

使用本地短变量名似乎是解决方案的一部分,但我喜欢有更多解释性的函数参数。嗯。因此,大幅缩短函数参数名称会导致(对我来说)代码不可读。

for l in range(n_labels):
t0 = d_t[l] if type(d_t) == numpy.ndarray) else d_t
t1 = r_t1[l] if type(r_t1) == numpy.ndarray else r_t1
t2 = r_t2[l] if type(r_t2) == numpy.ndarray else r_t1

那么我应该采用多行 if - else 赋值吗?它将极大地扩展和膨胀简单的逻辑。

for label in range(num_labels):
if type(drop_treshold) == numpy.ndarray):
d_tresh = drop_treshold[label]
else:
d_tresh = drop_treshold

if type(relabel_treshold1) == numpy.ndarray:
r_tresh1 = relabel_treshold1[label]
else:
d_tresh = relabel_treshold1

if type(relabel_treshold2) == numpy.ndarray:
r_tresh2 = relabel_treshold2[label]
else:
d_tresh = relabel_treshold2

(当然,我(sh/)也可以围绕所示示例重构整个代码...此示例片段来自带参数的函数,该函数可以是标量 float/int 或 1D numpy.array。如果它是一个数组它将把每个项目应用到每个标签,只是简单的标量,它将全局应用到所有标签)但这里再说一遍,Pythonic 方式是怎样的?何时开始更彻底地重构,何时保持不变 - 因为它有效?

最佳答案

这里的问题是你要对每个变量重复自己;如果对象是数组,则建立索引,否则直接使用该对象。我会在这里使用一个实用函数:

def threshold_label(ob, i):
# the threshold label can be a scalar or an array
return ob[i] if isinstance(ob, numpy.ndarray) else ob

for label in range(num_labels):
d_tresh = threshold(drop_treshold, label)
r_tresh1 = threshold(relabel_treshold1, label)
r_tresh2 = threshold(relabel_threshold2, label)

请注意,我使用了 isinstance()要测试每个对象的类型,请参阅 What are the differences between type() and isinstance()?

另一种选择是不测试数组每次迭代。测试一次,如果不是数组,则将标量float或int值转为期望长度的序列:

# turn scalar labels into sequences for easy iteration
if not isinstance(drop_threshold, numpy.ndarray):
drop_threshold = [drop_threshold] * num_labels
if not isinstance(relabel_treshold1, numpy.ndarray):
relabel_treshold1 = [relabel_treshold1] * num_labels
if not isinstance(drop_threshold, numpy.ndarray):
relabel_treshold2 = [relabel_treshold2] * num_labels

或者,再次使用辅助函数:

def ensure_sequence(ob, cnt):
# turn a scalar label value into a sequence if needed
return [ob] * cnt if not isinstance(ob, numpy.ndarray) else ob

drop_threshold = ensure_sequence(drop_threshold, num_labels)
relabel_treshold1 = ensure_sequence(relabel_treshold1, num_labels)
relabel_treshold2 = ensure_sequence(relabel_treshold2, num_labels)

此时您可以使用zip()进行迭代:

labels = zip(drop_threshold, relabel_threshold1, relabel_threshold2)
for d_thresh, r_thresh1, r_thresh2 in labels:
# ...

关于Pythonic 重构方式更长 0​​x104567910 if-condition-assignment (三元 If ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54944709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com