自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

從Python到NumPy,細(xì)說(shuō)最接近人類思維的in操作

開(kāi)發(fā) 后端
在Python語(yǔ)言中,in是一個(gè)使用頻率非常高的操作符,用于判斷對(duì)象是否位于字符串、元組、列表、集合或字典中。in操作和人的思維方式高度吻合,寫起來(lái)近乎于自然語(yǔ)言,充分體現(xiàn)了Python的哲學(xué)理念。

[[397119]]

本文轉(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é)理念。

  1. >>> 'or' in 'hello world' 
  2. True 
  3. >>> 5 in {1,2,3,4} 
  4. False 
  5. >>> 'age' in {'name':'Mike''age':18} 
  6. 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)題。

  1. >>> import numpy as np 
  2. >>> a = np.arange(9) 
  3. >>> a 
  4. array([0, 1, 2, 3, 4, 5, 6, 7, 8]) 
  5. >>> 5 in a 
  6. True 
  7. >>> 10 in a 
  8. False 

不過(guò),當(dāng)我嘗試在np.where()函數(shù)中使用in操作符的時(shí)候,出現(xiàn)了意外。

  1. >>> np.where(a>5) 
  2. (array([6, 7, 8], dtype=int64),) 
  3. >>> np.where(a%2==0) 
  4. (array([0, 2, 4, 6, 8], dtype=int64),) 
  5. >>> np.where(a in [2,3,5,7]) 
  6. Traceback (most recent call last): 
  7.   File "<pyshell#111>", line 1, in <module> 
  8.     np.where(a in [2,3,5,7]) 
  9. 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é)果。

  1. >>> np.where(a in np.array([2,3,5,7])) 
  2. (array([], dtype=int64),) 

難道NumPy不支持兩個(gè)數(shù)組之間的in操作嗎?不,強(qiáng)大到宇宙無(wú)敵的NumPy,怎么會(huì)不支持?jǐn)?shù)組之間的in操作呢?NumPy不但支持,而且支持得很好。

  1. >>> p = np.array([2,3,5,7]) # 質(zhì)數(shù)數(shù)組 
  2. >>> np.in1d(a, p) # 返回a的每一個(gè)元素是否是質(zhì)數(shù)的布爾數(shù)組 
  3. array([FalseFalse,  True,  TrueFalse,  TrueFalse,  TrueFalse]) 
  4. >>> np.where(np.in1d(a, p)) # 返回?cái)?shù)組a中質(zhì)數(shù)的索引序號(hào) 
  5. (array([2, 3, 5, 7], dtype=int64),) 
  6. >>> np.where(np.in1d(a, p), -1, a) # 返回?cái)?shù)組a中的質(zhì)數(shù)全部替換為-1的結(jié)果 
  7. array([ 0,  1, -1, -1,  4, -1,  6, -1,  8]) 

np.in1d()的參數(shù)如果是多維數(shù)組,將被自動(dòng)扁平化,且返回的布爾數(shù)組也是扁平化的一維數(shù)組。

  1. >>> np.in1d(a.reshape((3,3)), p) 
  2. array([FalseFalse,  True,  TrueFalse,  TrueFalse,  TrueFalse]) 

如果np.in1d()的參數(shù)是多維的,且期望返回和原數(shù)組結(jié)構(gòu)相同的布爾數(shù)組,則應(yīng)使用np.isin()函數(shù)。

  1. >>> np.isin(a.reshape((3,3)), p) 
  2. array([[FalseFalse,  True], 
  3.        [ TrueFalse,  True], 
  4.        [False,  TrueFalse]]) 
  5. >>> np.where(np.isin(a.reshape((3,3)), p)) 
  6. (array([0, 1, 1, 2], dtype=int64), array([2, 0, 2, 1], dtype=int64)) 

若是期望得到兩個(gè)數(shù)組的交集而不是交集元素的索引,下面兩種方式都可行。

  1. >>> a[np.where(np.isin(a, p))] 
  2. array([2, 3, 5, 7]) 
  3. >>> np.intersect1d(a, p) 
  4. array([2, 3, 5, 7]) 

第二種方式直接使用np.intersect1d()函數(shù)得到兩個(gè)數(shù)組的交集,且自動(dòng)排序。不過(guò),我更喜歡第一種方式。

責(zé)任編輯:武曉燕 來(lái)源: Python作業(yè)輔導(dǎo)員
相關(guān)推薦

2022-08-25 10:31:57

模型人工智能

2021-12-24 09:01:05

LeetCode三數(shù)之和算法

2021-07-19 14:37:04

AI 數(shù)據(jù)人工智能

2024-05-20 15:25:47

2023-03-26 21:03:54

GPT-4人工智能

2021-10-03 14:37:06

編程語(yǔ)言程序員代碼

2021-05-07 05:54:43

數(shù)據(jù)庫(kù)數(shù)據(jù)湖數(shù)據(jù)

2019-07-22 15:33:19

計(jì)算機(jī)互聯(lián)網(wǎng) 技術(shù)

2020-11-16 08:54:05

Google 開(kāi)源技術(shù)

2025-03-21 14:31:14

NumPyPython數(shù)組

2021-09-11 16:42:26

AndroidAndroid 12

2021-03-01 10:43:56

大數(shù)據(jù)人工智能

2013-08-20 13:34:02

創(chuàng)業(yè)極客

2021-02-28 13:57:51

大數(shù)據(jù)人工智能信息

2017-11-20 05:41:41

數(shù)組矩陣NumPy

2021-07-14 10:39:28

JqueryVue 編程

2021-07-26 16:08:36

AI Transformer人工智能

2016-01-05 09:42:39

2010-02-23 11:18:25

Python 操作符

2021-12-09 15:03:10

人工智能AI人類思維
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)