For loop heading depending on if statement (Python) -
i want write conditional loop, if array of length 1, use 1 loop heading, , if len(array) > 1 , == len(array2), use different loop heading, , if neither of these conditions true, quit error of choice. real issue don't want have if statement, , loops, when loops identical, except heading, , rather long, doubling code seems waste.
is there nice way have have meat of loop written once?
note: xarray , tarray multi-d numpy arrays ie) xarray = array([[1,2,3],[4,5,6]])
the snippit of code looks such:
if len(tarray) > 1 , len(xarray) == len(tarray): x,ts in zip(xarray,tarray): #stuff if len(tarray) == 1: x in xarray: #same stuff above loop else: print 'dimension mismatch -- quitting:' quit()
this should work:
if len(tarray) >= 1: res = zip(tarray, xarray) if len(tarray) == len(xarray) else xarray else: # error message each in res: # stuff
Comments
Post a Comment