I have the following function definition:
def rcsplit(arr):
if np.all(arr==0): return [] # if all zeros return
global res
arr = delrc(arr) # delete leading/trailing rows/cols with all zeros
indr = np.where(np.all(arr==0,axis=1))[0]
indc = np.where(np.all(arr==0,axis=0))[0]
if not indr and not indc: # If no further split possible return
res.append(arr)
return
arr=np.delete(arr,indr,axis=0) #delete empty rows in between non empty rows
arr=np.delete(arr,indc,axis=1) #delete empty cols in between non empty cols
arr=np.split(arr,indc,axis=1) # split on empty (all zeros) cols
arr2=[]
for i in arr:
z=delrc(i)
arr2.extend(np.split(z,indr,axis=0)) # split on empty (all zeros) rows
for i in arr2:
rcsplit(np.array(i))
The problem is I receive the following error:
NameError: global name 'res' is not defined
But this exact code works on other consoles. Is it my Python 2.7?
1 Answer 1
It seems that the global variable res is not defined before the function, maybe it is defined in the other console.
answered Nov 30, 2014 at 20:30
Chedy2149
3,0914 gold badges37 silver badges57 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py
resfunction argument with a default value ofNone, then in the function check to see if that's its value and initialize it to[]if so. When the function calls itself recursively, pass the current value ofresin the call (to override the default value ofNone).