A few days back i asked a question about repeating the same structure of code over and over again(last question).
Somebody say to look at Try-Except method (less checks save time).
I tried it out but till know not with a result i can work with...
I have created a example i think it explains my problem and is more easy to understand as my last one...
def simpleTry(a):
import numpy as np
try:
return(np.sum(a))
except np.ndim(a) > 1:
a_shape = np.shape(a)
b = []
for a_line in range(a_shape[0]):
print(a_line, np.sum(a[a_line]))
b = np.append(b, np.sum(a[a_line]))
return(b)
def simpleIf(a):
import numpy as np
if np.ndim(a) == 1:
return(np.sum(a))
else:
a_shape = np.shape(a)
b = []
for a_line in range(a_shape[0]):
print(a_line, np.sum(a[a_line]))
b = np.append(b, np.sum(a[a_line]))
return(b)
test_1 = np.array([[1, 2, 3, 4], [0, 9, 8, 7], [5, 7, 4, 8]])
test_2 = np.array([1, 2, 3, 4])
result_1 = simpleTry(b)
result_2 = simpleIf(d)
The 'simpleIf' is a function as i use it now. put a ndim array in and i get the results back.
when i changed to Try-except like in 'simpleTry' i get a single result. The except don't start working because of the normal function don't break. The only method i let the code break is creating a (python)function that breaks... and i'm back by my if...
So Am i stucked with my if function or are there other methods??
I know numpy.sum() have an axis propperty i used a 'if function with for loop' for that... but that's just my problem...
Aucun commentaire:
Enregistrer un commentaire