【问题标题】:Python error: Valueerror-need-more-than-1-value-to-unpackPython 错误:Valueerror-need-more-than-1-value-to-unpack
【发布时间】:2016-03-03 09:42:49
【问题描述】:

当我在 python 中运行这段代码时:

lat, lon = f.variables['latitude'], f.variables['longitude']
latvals = lat[:]; lonvals = lon[:] 
def getclosest_ij(lats,lons,latpt,lonpt):
dist_sq = (lats-latpt)**2 + (lons-lonpt)**2  
minindex_flattened = dist_sq.argmin()    
return np.unravel_index(minindex_flattened, lats.shape)
iy_min, ix_min = getclosest_ij(latvals, lonvals, 46.1514, 20.0846)

它得到以下错误:

ValueError Traceback(最近一次调用最后一次)

模块中的ipython-input-104-3ba92bea5d48

11     return np.unravel_index(minindex_flattened, lats.shape)
12     iy_min, ix_min = getclosest_ij(latvals, lonvals, 46.1514, 20.0846)

ValueError: 需要超过 1 个值才能解压

这是什么意思?我该如何解决? 我会读一个 NetCDF 文件,它由总水柱数据组成,维度为:时间(124)、纬度(15)和经度(15)。我会为特定点(纬度,经度)和时间分配 tcw 的数量。我尝试使用上面的代码来解决我的任务的第一部分,以评估特定坐标的 tcw,但没有奏效。

提前感谢您的帮助。

【问题讨论】:

    标签: python python-3.x netcdf


    【解决方案1】:

    在python中你可以写

    var1, var2 = (1, 2) # = iterable with 2 items
    

    这会将 1 存储在 var1 中,将 2 存储在 var2 中。 此功能称为解包。

    因此,您的代码抛出的错误意味着函数 getclosest_ij 返回 一个 值,而不是您需要将它们解压缩到 iy_minix_min 中的 2 个值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-26
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 1970-01-01
      • 2018-06-28
      • 1970-01-01
      相关资源
      最近更新 更多