I want to write a conditional for loop, where if an array is of length 1, use one for loop heading, and if this len(array) > 1 and == len(array2), use a different for loop heading, and if neither of these conditions are true, quit with an error of my choice. The real issue is that I don't want to have this if statement, and then the for loops, when the for loops actually are IDENTICAL, except for the heading, and rather long, so doubling the code seems like a waste.
Is there a nice way to do this where I only have to have the meat of the for loop written once?
Note: xarray and tarray are multi-d numpy arrays ie) xarray = array([[1,2,3],[4,5,6]])
The snippit of code looks as such:
if len(tarray) > 1 and len(xarray) == len(tarray):
for x,ts in zip(xarray,tarray):
#stuff
if len(tarray) == 1:
for x in xarray:
#same stuff as above for loop
else:
print 'Dimension Mismatch -- Quitting:'
quit()