我有以下功能定义:
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))
问题是我收到以下错误:
NameError: global name 'res' is not defined
但是这个确切的代码适用于其他控制台。这是我的Python 2.7吗?
答案 0 :(得分:1)
似乎全局变量res
未在函数之前定义,可能是在其他控制台中定义的。