您可以通過(guò)迭代字典中的每個(gè)項(xiàng)目來(lái)做到這一點(diǎn),但是在大型數(shù)據(jù)集中它可能效率低下。def get_key(data, query): for key, value in data.items(): if query in value[0][1]: return key return 'Not Found'get_key(dictonary, word)然后,即使您的查找未能找到匹配項(xiàng),您也可以調(diào)用您的函數(shù)并返回結(jié)果。# Note i changed the name of the dictionary to dicton, as dict shouldn't be used as a variable nameprint(get_key(dicton, 'three'))print(get_key(dicton, 'seven'))print(get_key(dicton, 'four'))#a#Not Found#b我有一些 0.2 度和 1 度分辨率的虛擬數(shù)據(jù)。我想對(duì) foo 進(jìn)行二次采樣,使其與 foo1 的比例相同。有什么簡(jiǎn)單的方法可以以某種方式平均和重新排列我的經(jīng)緯度坐標(biāo)嗎?import pandas as pdimport xarray as xrimport matplotlib.pyplot as plt#Set at 0.2 degree grids ishfreq=20lats=240lons=1020time=pd.date_range('2000-01',periods=freq,freq='Y')data=np.random.rand(freq,lats,lons)lat=np.linspace(-19.5,19.5,lats)lon=np.linspace(120,290,lons)foo = xr.DataArray(data, coords=[time, lat,lon], dims=['time', 'lat','lon'])foo.sel(time='2005',method='nearest').plot()plt.show()#Set at 1 degree gridsfreq1=20lats1=40 #Factor of 6 differencelons1=170time1=pd.date_range('2000-01',periods=freq1,freq='Y')data1=np.random.rand(freq1,lats1,lons1)lat1=np.linspace(-19.5,19.5,lats1)lon1=np.linspace(120,290,lons1)foo1 = xr.DataArray(data1, coords=[time1, lat1,lon1], dims=['time', 'lat','lon'])foo1.sel(time='2005',method='nearest').plot()plt.show()
2 回答

慕工程0101907
TA貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊
我決定最簡(jiǎn)單的方法是使用 foo1 網(wǎng)格進(jìn)行解釋。
因此:
foo2=foo.interp(lat=lat1).interp(lon=lon1)
foo2.sel(time='2005',method='nearest').plot()
應(yīng)生成準(zhǔn)確的子采樣網(wǎng)格圖。
添加回答
舉報(bào)
0/150
提交
取消