我有一个2d数组(不同长度)的列表,我需要通过列表理解有效地应用某些函数
由于这还不够快,列表理解需要并行化
保持切片(或“子阵列”)顺序的正确方法是什么
def get_slice_max(arr):
'''
get the slice, but replace every element with the maximum value that has occoured till(including) the iter so far.
'''
result = [arr[0]]
for i in range(1, len(arr)):
result.append(max(result[-1], arr[i]))
return result
result = [get_slice_max(slice_) for slice_ in a]
可复制样本:
a = [ np.array(range(1, random.randint(3, 8))) for x in range(10000)]
编辑: 我需要对列表的理解进行并行化,比如:
temp = np.random.randint(1, high=100, size=10) # determines the sizes of the subarrays
A,B,C = [ np.randint(0, high=1, size=x) for x in temp],
[ np.random.uniform(size=x) for x in temp],
[ np.random.uniform(size=x) for x in temp]
result = [ [y if x==1 else z for x, y, z in zip(a, b, c)]
for a, b, c in zip(A, B, C,) ]
temp = np.random.randint(1, high=100, size=10) # determines the sizes of the subarrays
D, E = [ np.random.uniform(size=x) for x in temp], [ np.randint(0, high=1, size=x) for x in temp]
[ [ x/y for x,y in zip(d,np.maximum.accumulate(get_slice_max(e))] for d, e in zip(D, E) ]
使用
numpy.maximum.accumulate
:输出:
验证:
基准测试(大约快6倍):
相关问题 更多 >
编程相关推荐