從Python到NumPy,細(xì)說(shuō)最接近人類思維的in操作
本文轉(zhuǎn)載自微信公眾號(hào)「 Python作業(yè)輔導(dǎo)員」,作者天元浪子 。轉(zhuǎn)載本文請(qǐng)聯(lián)系 Python作業(yè)輔導(dǎo)員公眾號(hào)。
在Python語(yǔ)言中,in是一個(gè)使用頻率非常高的操作符,用于判斷對(duì)象是否位于字符串、元組、列表、集合或字典中。in操作和人的思維方式高度吻合,寫起來(lái)近乎于自然語(yǔ)言,充分體現(xiàn)了Python的哲學(xué)理念。
- >>> 'or' in 'hello world'
- True
- >>> 5 in {1,2,3,4}
- False
- >>> 'age' in {'name':'Mike', 'age':18}
- True
有趣的是,除了R、javascript、SQL外,包括C/C++在內(nèi)的主流語(yǔ)言幾乎都不支持in操作。這或許可以解釋為什么Python語(yǔ)言被認(rèn)為是最容易學(xué)習(xí)的編程語(yǔ)言。
習(xí)慣了使用Python的in操作符,有時(shí)就會(huì)自然而然地應(yīng)用到NumPy數(shù)組操作上。比如,下面的寫法看起來(lái)沒(méi)有任何問(wèn)題。
- >>> import numpy as np
- >>> a = np.arange(9)
- >>> a
- array([0, 1, 2, 3, 4, 5, 6, 7, 8])
- >>> 5 in a
- True
- >>> 10 in a
- False
不過(guò),當(dāng)我嘗試在np.where()函數(shù)中使用in操作符的時(shí)候,出現(xiàn)了意外。
- >>> np.where(a>5)
- (array([6, 7, 8], dtype=int64),)
- >>> np.where(a%2==0)
- (array([0, 2, 4, 6, 8], dtype=int64),)
- >>> np.where(a in [2,3,5,7])
- Traceback (most recent call last):
- File "<pyshell#111>", line 1, in <module>
- np.where(a in [2,3,5,7])
- ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
使用a>5或者a%2==0作為條件,np.where()函數(shù)沒(méi)有問(wèn)題,但是,使用a in [2,3,5,7],np.where()就會(huì)拋出異常。即便寫成下面這樣,也不能得到期望的結(jié)果。
- >>> np.where(a in np.array([2,3,5,7]))
- (array([], dtype=int64),)
難道NumPy不支持兩個(gè)數(shù)組之間的in操作嗎?不,強(qiáng)大到宇宙無(wú)敵的NumPy,怎么會(huì)不支持?jǐn)?shù)組之間的in操作呢?NumPy不但支持,而且支持得很好。
- >>> p = np.array([2,3,5,7]) # 質(zhì)數(shù)數(shù)組
- >>> np.in1d(a, p) # 返回a的每一個(gè)元素是否是質(zhì)數(shù)的布爾數(shù)組
- array([False, False, True, True, False, True, False, True, False])
- >>> np.where(np.in1d(a, p)) # 返回?cái)?shù)組a中質(zhì)數(shù)的索引序號(hào)
- (array([2, 3, 5, 7], dtype=int64),)
- >>> np.where(np.in1d(a, p), -1, a) # 返回?cái)?shù)組a中的質(zhì)數(shù)全部替換為-1的結(jié)果
- array([ 0, 1, -1, -1, 4, -1, 6, -1, 8])
np.in1d()的參數(shù)如果是多維數(shù)組,將被自動(dòng)扁平化,且返回的布爾數(shù)組也是扁平化的一維數(shù)組。
- >>> np.in1d(a.reshape((3,3)), p)
- array([False, False, True, True, False, True, False, True, False])
如果np.in1d()的參數(shù)是多維的,且期望返回和原數(shù)組結(jié)構(gòu)相同的布爾數(shù)組,則應(yīng)使用np.isin()函數(shù)。
- >>> np.isin(a.reshape((3,3)), p)
- array([[False, False, True],
- [ True, False, True],
- [False, True, False]])
- >>> np.where(np.isin(a.reshape((3,3)), p))
- (array([0, 1, 1, 2], dtype=int64), array([2, 0, 2, 1], dtype=int64))
若是期望得到兩個(gè)數(shù)組的交集而不是交集元素的索引,下面兩種方式都可行。
- >>> a[np.where(np.isin(a, p))]
- array([2, 3, 5, 7])
- >>> np.intersect1d(a, p)
- array([2, 3, 5, 7])
第二種方式直接使用np.intersect1d()函數(shù)得到兩個(gè)數(shù)組的交集,且自動(dòng)排序。不過(guò),我更喜歡第一種方式。