是否有一种方法来减少冗余时声明这样的变量?


max_vert = list(itertools.accumulate(map(len, vertices)))
min_vert = [0] + max_vert[:-1]
num_indices = list(itertools.accumulate(map(len, indices)))
first_index = [0] + num_indices[:-1]

我有这段代码,我想知道是否有一种方法可以减少冗余并使其更紧凑。有办法吗?最紧凑的方法是什么?如上所示,声明的所有变量都非常相似。如果有一种更紧凑的方法来做同样的事情,是不是更好?它会是什么样子?

创建和重用辅助函数:

def max_min(stuff):
max_stuff = list(itertools.accumulate(map(len, stuff)))
min_stuff = [0] + max_stuff [:-1]
return max_stuff , min_stuff 
max_vert, min_vert = max_min(vertices)
num_indices, first_index = max_min(indices)
# and reuse again if you have more similar definitions / computations ...

最新更新