【问题标题】:How to access a cell of a grid using a 2d array in python?如何在 python 中使用二维数组访问网格的单元格?
【发布时间】:2015-03-03 23:13:21
【问题描述】:

我正在尝试使用 python 将一个单元格转换为一个值,用于 NetCDF 数据集。我是 python 新手,我不明白我在这里做错了什么。我有一个二维数组,我通过遍历数据集 dataArr 来给出四个点。 我使用 Lerp 方法进行计算。不知何故,当我通过 3 个值时,它似乎认为它超过 3。 我在这里做错了什么?谢谢你的建议。

这是我得到的错误>> self.Lerp((dataArr[i][j]),(dataArr[i+1][j]),fraction) 正好需要 3 个参数(给定 4 个)

def compute(self,varval):      
        vars=self.data.variables
        for var in vars:
            if var==varval:
                ntimes, ny, nx=vars[var].shape #inherit the method above.
        print(ntimes, ny, nx)
        #create the old computational grid.
        computational_grid=np.zeros((ny,nx),dtype=int) 
        fraction=.5 
        newnx,newny =(nx*fraction,ny*fraction)
        new_computational_grid=np.zeros((newny,newnx),dtype=int)
        phy_value_arr=self.get_data(varval)
        t=10 #send this t value with coords
        dataArr=self.data.variables['tos'][t]     
        for i in range(0,(ny-1),1):
            for j in range(0,(nx-1),1):
               a=self.Lerp((dataArr[i][j]),(dataArr[i+1][j]),fraction)
               b=self.Lerp((dataArr[i][j]),(dataArr[i+1][j]),fraction)
               self.tempY.append(self.Lerp(a,b,fraction))
       tempY.reshape(newnx,newny)
       pcolormesh(self.tempY)
       colorbar()

def Lerp( _a, _b, _t) :
      return _a+(_b-_a)*_t

【问题讨论】:

    标签: python arrays numpy netcdf


    【解决方案1】:

    你像这样称呼 Lerp:

    self.Lerp(a,b,fraction)
    

    根据定义,它会将 self 作为第一个参数。因此,您总共将拥有四个。但是,您将 Lerp 定义为

    def Lerp( _a, _b, _t) : #<- this takes only three arguments
    

    我认为应该是:

    def Lerp(self, _a, _b, _t) : #<- this takes four, and self is first one
    

    【讨论】:

    • 感谢您的陈述!你节省了我很多时间。我假设我访问数组的方式很好..
    • 我没有看数组定义或访问,所以不知道。 Lerp 的定义是不正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-02
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多