本文目錄一覽:
python常用函數
1、complex()
返回一個形如 a+bj 的複數,傳入參數分為三種情況:
參數為空時,返回0j;參數為字元串時,將字元串表達式解釋為複數形式並返回;參數為兩個整數(a,b)時,返回 a+bj;參數只有一個整數 a 時,虛部 b 默認為0,函數返回 a+0j。
2、dir()
不提供參數時,返回當前本地範圍內的名稱列表;提供一個參數時,返回該對象包含的全部屬性。
3、divmod(a,b)
a — 代表被除數,整數或浮點數;b — 代表除數,整數或浮點數;根據 除法運算 計算 a,b 之間的商和餘數,函數返回一個元組(p,q) ,p 代表商 a//b ,q 代表餘數 a%b。
4、enumerate(iterable,start=0)
iterable — 一個可迭代對象,列表、元組序列等;start — 計數索引值,默認初始為0『該函數返回枚舉對象是個迭代器,利用 next() 方法依次返回元素值,每個元素以元組形式存在,包含一個計數元素(起始為 start )和 iterable 中對應的元素值。
Python氣象數據處理與繪圖(2):常用數據計算方法
對於氣象繪圖來講,第一步是對數據的處理,通過各類公式,或者統計方法將原始數據處理為目標數據。
按照氣象統計課程的內容,我給出了一些常用到的統計方法的對應函數:
在計算氣候態,區域平均時均要使用到求均值函數,對應NCL中的dim_average函數,在python中通常使用np.mean()函數
numpy.mean(a, axis, dtype)
假設a為[time,lat,lon]的數據,那麼
需要特別注意的是,氣象數據中常有缺測,在NCL中,使用求均值函數會自動略過,而在python中,當任意一數與缺測(np.nan)計算的結果均為np.nan,比如求[1,2,3,4,np.nan]的平均值,結果為np.nan
因此,當數據存在缺測數據時,通常使用np.nanmean()函數,用法同上,此時[1,2,3,4,np.nan]的平均值為(1+2+3+4)/4 = 2.5
同樣的,求某數組最大最小值時也有np.nanmax(), np.nanmin()函數來補充np.max(), np.min()的不足。
其他很多np的計算函數也可以通過在前邊加『nan』來使用。
另外,
也可以直接將a中缺失值全部填充為0。
np.std(a, axis, dtype)
用法同np.mean()
在NCL中有直接求數據標準化的函數dim_standardize()
其實也就是一行的事,根據需要指定維度即可。
皮爾遜相關係數:
相關可以說是氣象科研中最常用的方法之一了,numpy函數中的np.corrcoef(x, y)就可以實現相關計算。但是在這裡我推薦scipy.stats中的函數來計算相關係數:
這個函數缺點和有點都很明顯,優點是可以直接返回相關係數R及其P值,這避免了我們進一步計算置信度。而缺點則是該函數只支持兩個一維數組的計算,也就是說當我們需要計算一個場和一個序列的相關時,我們需要循環來實現。
其中a[time,lat,lon],b[time]
(NCL中為regcoef()函數)
同樣推薦Scipy庫中的stats.linregress(x,y)函數:
slop: 回歸斜率
intercept:回歸截距
r_value: 相關係數
p_value: P值
std_err: 估計標準誤差
直接可以輸出P值,同樣省去了做置信度檢驗的過程,遺憾的是仍需同相關係數一樣循環計算。
python三個重要的內置函數(map, filter,reduce)-
map函數第一個參數是一個函數function,第二個參數是一個可迭代的對象iterable,他的功能是將可迭代對象iterable裡面的每一項都應用到函數function中,然後返回一個迭代器。
可迭代器裡面有多少個元素則結果就包含多少個元素
filter() 函數用於過濾序列,過濾掉不符合條件的元素,返回由符合條件元素組成的新列表。
該接收兩個參數,第一個為函數,第二個為序列,序列的每個元素作為參數傳遞給函數進行判斷,然後返回 True 或 False,最後將返回 True 的元素放到新列表中。
reduce() 函數會對參數序列中元素進行累積。
函數將一個數據集合(鏈表,元組等)中的所有數據進行下列操作:用傳給 reduce 中的函數 function(有兩個參數)先對集合中的第 1、2 個元素進行操作,得到的結果再與第三個數據用 function 函數運算,最後得到一個結果。
python字典操作函數
字典是一種通過名字或者關鍵字引用的得數據結構,其鍵可以是數字、字元串、元組,這種結構類型也稱之為映射。字典類型是Python中唯一內建的映射類型,基本的操作包括如下:
(1)len():返回字典中鍵—值對的數量;
(2)d[k]:返回關鍵字對於的值;
(3)d[k]=v:將值關聯到鍵值k上;
(4)del d[k]:刪除鍵值為k的項;
(5)key in d:鍵值key是否在d中,是返回True,否則返回False。
(6)clear函數:清除字典中的所有項
(7)copy函數:返回一個具有相同鍵值的新字典;deepcopy()函數使用深複製,複製其包含所有的值,這個方法可以解決由於副本修改而使原始字典也變化的問題
(8)fromkeys函數:使用給定的鍵建立新的字典,鍵默認對應的值為None
(9)get函數:訪問字典成員
(10)has_key函數:檢查字典中是否含有給出的鍵
(11)items和iteritems函數:items將所有的字典項以列表方式返回,列表中項來自(鍵,值),iteritems與items作用相似,但是返回的是一個迭代器對象而不是列表
(12)keys和iterkeys:keys將字典中的鍵以列表形式返回,iterkeys返回鍵的迭代器
(13)pop函數:刪除字典中對應的鍵
(14)popitem函數:移出字典中的項
(15)setdefault函數:類似於get方法,獲取與給定鍵相關聯的值,也可以在字典中不包含給定鍵的情況下設定相應的鍵值
(16)update函數:用一個字典更新另外一個字典
(17) values和itervalues函數:values以列表的形式返回字典中的值,itervalues返回值得迭代器,由於在字典中值不是唯一的,所以列表中可以包含重複的元素
一、字典的創建
1.1 直接創建字典
d={‘one’:1,’two’:2,’three’:3}
printd
printd[‘two’]
printd[‘three’]
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{‘three’:3,’two’:2,’one’:1}
1.2 通過dict創建字典
# _*_ coding:utf-8 _*_
items=[(‘one’,1),(‘two’,2),(‘three’,3),(‘four’,4)]
printu’items中的內容:’
printitems
printu’利用dict創建字典,輸出字典內容:’
d=dict(items)
printd
printu’查詢字典中的內容:’
printd[‘one’]
printd[‘three’]
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
items中的內容:
[(‘one’,1), (‘two’,2), (‘three’,3), (‘four’,4)]
利用dict創建字典,輸出字典內容:
{‘four’:4,’three’:3,’two’:2,’one’:1}
查詢字典中的內容:
或者通過關鍵字創建字典
# _*_ coding:utf-8 _*_
d=dict(one=1,two=2,three=3)
printu’輸出字典內容:’
printd
printu’查詢字典中的內容:’
printd[‘one’]
printd[‘three’]
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
輸出字典內容:
{‘three’:3,’two’:2,’one’:1}
查詢字典中的內容:
二、字典的格式化字元串
# _*_ coding:utf-8 _*_
d={‘one’:1,’two’:2,’three’:3,’four’:4}
printd
print”three is %(three)s.”%d
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{‘four’:4,’three’:3,’two’:2,’one’:1}
threeis3.
三、字典方法
3.1 clear函數:清除字典中的所有項
# _*_ coding:utf-8 _*_
d={‘one’:1,’two’:2,’three’:3,’four’:4}
printd
d.clear()
printd
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{‘four’:4,’three’:3,’two’:2,’one’:1}
{}
請看下面兩個例子
3.1.1
# _*_ coding:utf-8 _*_
d={}
dd=d
d[‘one’]=1
d[‘two’]=2
printdd
d={}
printd
printdd
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{‘two’:2,’one’:1}
{}
{‘two’:2,’one’:1}
3.1.2
# _*_ coding:utf-8 _*_
d={}
dd=d
d[‘one’]=1
d[‘two’]=2
printdd
d.clear()
printd
printdd
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{‘two’:2,’one’:1}
{}
{}
3.1.2與3.1.1唯一不同的是在對字典d的清空處理上,3.1.1將d關聯到一個新的空字典上,這種方式對字典dd是沒有影響的,所以在字典d被置空後,字典dd裡面的值仍舊沒有變化。但是在3.1.2中clear方法清空字典d中的內容,clear是一個原地操作的方法,使得d中的內容全部被置空,這樣dd所指向的空間也被置空。
3.2 copy函數:返回一個具有相同鍵值的新字典
# _*_ coding:utf-8 _*_
x={‘one’:1,’two’:2,’three’:3,’test’:[‘a’,’b’,’c’]}
printu’初始X字典:’
printx
printu’X複製到Y:’
y=x.copy()
printu’Y字典:’
printy
y[‘three’]=33
printu’修改Y中的值,觀察輸出:’
printy
printx
printu’刪除Y中的值,觀察輸出’
y[‘test’].remove(‘c’)
printy
printx
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
初始X字典:
{‘test’: [‘a’,’b’,’c’],’three’:3,’two’:2,’one’:1}
X複製到Y:
Y字典:
{‘test’: [‘a’,’b’,’c’],’one’:1,’three’:3,’two’:2}
修改Y中的值,觀察輸出:
{‘test’: [‘a’,’b’,’c’],’one’:1,’three’:33,’two’:2}
{‘test’: [‘a’,’b’,’c’],’three’:3,’two’:2,’one’:1}
刪除Y中的值,觀察輸出
{‘test’: [‘a’,’b’],’one’:1,’three’:33,’two’:2}
{‘test’: [‘a’,’b’],’three’:3,’two’:2,’one’:1}
註:在複製的副本中對值進行替換後,對原來的字典不產生影響,但是如果修改了副本,原始的字典也會被修改。deepcopy函數使用深複製,複製其包含所有的值,這個方法可以解決由於副本修改而使原始字典也變化的問題。
# _*_ coding:utf-8 _*_
fromcopyimportdeepcopy
x={}
x[‘test’]=[‘a’,’b’,’c’,’d’]
y=x.copy()
z=deepcopy(x)
printu’輸出:’
printy
printz
printu’修改後輸出:’
x[‘test’].append(‘e’)
printy
printz
運算輸出:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
輸出:
{‘test’: [‘a’,’b’,’c’,’d’]}
{‘test’: [‘a’,’b’,’c’,’d’]}
修改後輸出:
{‘test’: [‘a’,’b’,’c’,’d’,’e’]}
{‘test’: [‘a’,’b’,’c’,’d’]}
3.3 fromkeys函數:使用給定的鍵建立新的字典,鍵默認對應的值為None
# _*_ coding:utf-8 _*_
d=dict.fromkeys([‘one’,’two’,’three’])
printd
運算輸出:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{‘three’:None,’two’:None,’one’:None}
或者指定默認的對應值
# _*_ coding:utf-8 _*_
d=dict.fromkeys([‘one’,’two’,’three’],’unknow’)
printd
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{‘three’:’unknow’,’two’:’unknow’,’one’:’unknow’}
3.4 get函數:訪問字典成員
# _*_ coding:utf-8 _*_
d={‘one’:1,’two’:2,’three’:3}
printd
printd.get(‘one’)
printd.get(‘four’)
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{‘three’:3,’two’:2,’one’:1}
1
None
註:get函數可以訪問字典中不存在的鍵,當該鍵不存在是返回None
3.5 has_key函數:檢查字典中是否含有給出的鍵
# _*_ coding:utf-8 _*_
d={‘one’:1,’two’:2,’three’:3}
printd
printd.has_key(‘one’)
printd.has_key(‘four’)
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{‘three’:3,’two’:2,’one’:1}
True
False
3.6 items和iteritems函數:items將所有的字典項以列表方式返回,列表中項來自(鍵,值),iteritems與items作用相似,但是返回的是一個迭代器對象而不是列表
# _*_ coding:utf-8 _*_
d={‘one’:1,’two’:2,’three’:3}
printd
list=d.items()
forkey,valueinlist:
printkey,’:’,value
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{‘three’:3,’two’:2,’one’:1}
three :3
two :2
one :1
# _*_ coding:utf-8 _*_
d={‘one’:1,’two’:2,’three’:3}
printd
it=d.iteritems()
fork,vinit:
print”d[%s]=”%k,v
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{‘three’:3,’two’:2,’one’:1}
d[three]=3
d[two]=2
d[one]=1
3.7 keys和iterkeys:keys將字典中的鍵以列表形式返回,iterkeys返回鍵的迭代器
# _*_ coding:utf-8 _*_
d={‘one’:1,’two’:2,’three’:3}
printd
printu’keys方法:’
list=d.keys()
printlist
printu’\niterkeys方法:’
it=d.iterkeys()
forxinit:
printx
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{‘three’:3,’two’:2,’one’:1}
keys方法:
[‘three’,’two’,’one’]
iterkeys方法:
three
two
one
3.8 pop函數:刪除字典中對應的鍵
# _*_ coding:utf-8 _*_
d={‘one’:1,’two’:2,’three’:3}
printd
d.pop(‘one’)
printd
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{‘three’:3,’two’:2,’one’:1}
{‘three’:3,’two’:2}
3.9 popitem函數:移出字典中的項
# _*_ coding:utf-8 _*_
d={‘one’:1,’two’:2,’three’:3}
printd
d.popitem()
printd
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{‘three’:3,’two’:2,’one’:1}
{‘two’:2,’one’:1}
3.10 setdefault函數:類似於get方法,獲取與給定鍵相關聯的值,也可以在字典中不包含給定鍵的情況下設定相應的鍵值
# _*_ coding:utf-8 _*_
d={‘one’:1,’two’:2,’three’:3}
printd
printd.setdefault(‘one’,1)
printd.setdefault(‘four’,4)
printd
運算結果:
{‘three’:3,’two’:2,’one’:1}
{‘four’:4,’three’:3,’two’:2,’one’:1}
3.11 update函數:用一個字典更新另外一個字典
# _*_ coding:utf-8 _*_
d={
‘one’:123,
‘two’:2,
‘three’:3
}
printd
x={‘one’:1}
d.update(x)
printd
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
{‘three’:3,’two’:2,’one’:123}
{‘three’:3,’two’:2,’one’:1}
3.12 values和itervalues函數:values以列表的形式返回字典中的值,itervalues返回值得迭代器,由於在字典中值不是唯一的,所以列表中可以包含重複的元素
# _*_ coding:utf-8 _*_
d={
‘one’:123,
‘two’:2,
‘three’:3,
‘test’:2
}
printd.values()
運算結果:
=======RESTART: C:\Users\Mr_Deng\Desktop\test.py=======
[2,3,2,123]
原創文章,作者:EABX,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/133719.html