2016-10-28 77 views
0

在這部分代碼中,我定義了一個函數來對感興趣的區域進行子集。但是,我想在後面的另一個函數中使用變量latselect和lonselect。所以,我有:我如何評估一個局部變量函數?

def DatasetToSubset(file, LatUpbound, LatLowBound, LonUpBound, LonLowBound): 
    nc=netCDF4.Dataset(file) 
    lats=nc.variables['lat'][:]; lons=nc.variables['lon'][:] 
    latselect=np.logical_and(lats > LatLowBound, lats < LatUpBound)  
    lonselect=np.logical_and(lon > LonLowBound, lon < LonUpBound) 
    data=nc.variables['Runoff'][1000, latselect, lonselect] 
    return data; return latselect; return lonselect 

回答

1

一旦功能達到return聲明,它返回值,並立即終止,這意味着隨後的兩個語句將不會執行。您可以返回三個值的元組,這樣

def DatasetToSubset(file, LatUpbound, LatLowBound, LonUpBound, LonLowBound): 
    nc=netCDF4.Dataset(file) 
    lats=nc.variables['lat'][:]; lons=nc.variables['lon'][:] 
    latselect=np.logical_and(lats > LatLowBound, lats < LatUpBound)  
    lonselect=np.logical_and(lon > LonLowBound, lon < LonUpBound) 
    data=nc.variables['Runoff'][1000, latselect, lonselect] 
    return (data, latselect, lonselect) 

,當你調用這個函數,你可以解開三個值這樣

(a, b, c) = DatasetToSubset(...) 

a將持有的data值,blatselectclonselect