å¨numpyçndarrayç±»åä¸ï¼ä¼¼ä¹æ²¡æç´æ¥è¿åç¹å®ç´¢å¼çæ¹æ³ï¼æåªæ¾å°äºwhereå½æ°ï¼ä½æ¯whereå½æ°å¯¹äºå¯»æ¾æ个ç¹å®å¼å¯¹åºçç´¢å¼å¾æç¨ï¼å¯¹äºè¿åä¸å®åºé´å å¼çç´¢å¼ä¸æ¯å¾ææï¼è³å°æ没æå¼æç½åºè¯¥å¦ä½æä½ãä¸é¢å 说ä¸ä¸whereå½æ°çç¨æ³å§ã
ï¼1ï¼whereå½æ°ç使ç¨åºæ¯ï¼
ä¾å¦ç°å¨æçæäºä¸ä¸ªæ°ç»ï¼
import numpy as np
arr=np.array([1,1,1,134,45,3,46,45,65,3,23424,234,12,12,3,546,1,2])
ç°å¨arræ¯ä¸ä¸ªå«æ18个å ç´ çndarrayç±»åçæ°ç»ï¼åé¢å°±ç¨æ°ç»æ¥ç§°å¼å®å§ãåå¦ææ³è¿åæ°ç»ä¸ææå ç´ å¼ä¸º3æ对åºçç´¢å¼ä½ç½®ï¼é£ä¹éè¿whereå½æ°å¯ä»¥å¾è½»æ¾å°å®æã
print np.where(arr==3)
å®ä¼è¿åä¸ä¸ªå å«ææå¼ä¸º3对åºçç´¢å¼ä½ç½®çå ç»ï¼å¦ä¸å¾ï¼
å¯ä»¥çå°å¨ç´¢å¼ä¸º5ã9ã14çå°æ¹æ对åºçå ç´ å¼ä¸º3ãéè¿è¿æ ·çæ¹å¼å¯ä»¥è½»æ¾è¾¾å°ç®çãä½æ¯å¯¹äºä¸å®ååèå´å çç´¢å¼è¿æ ·å°±ä¸è¡äºãä¸é¢çæ¹æ³æ¯èªå·±æ³åºæ¥çæè¡·åæ³ï¼æ¯è¾ç¬¨ï¼é«ææ¸ å·ã
ï¼2ï¼éè¿ä¸ä¸ªè¾ å©çæ°ç»æ¥è§£å³ä¸å®èå´å å¼ç´¢å¼çæ¥æ¾
æ们建ä¸ä¸ªæ è¯å ç´ ç´¢å¼çæ°ç»ï¼ç¶åéè¿å®æ¥æ¾ç¤ºç¬¦åæ¡ä»¶çå ç´ å¯¹åºçç´¢å¼ãè¿æ¯åæçæ°ç»ï¼åå¦æç°å¨è¦è¿åå ç´ å¼å¨3å°100ä¹é´å¼çç´¢å¼ãæå¯ä»¥çæä¸ä¸ªåarrç¸å大å°çæ°ç»ï¼ç¶åé¦å éè¿ä¸æ¬¡çéæ¾å°å¤§äº3çå ç´ å¯¹åºç´¢å¼çæ°ç»ï¼ç»§èå¨å ¶ä¸å次çéï¼æç»å¾å°æ³è¦çç»æã代ç å¦ä¸ï¼
b=np.arange(len(arr))#çæåarrç¸åé¿åº¦çæ°ç»
c=b[arr>3]#cåæ¾çå°±æ¯arrä¸å¤§äº3çå
ç´ å¯¹åºçç´¢å¼
#æåéè¿éåcæ°ç»ï¼éæ©3å°100ä¹é´çå¼æå°åºæ¥
for i in range(len(c)):
if arr[c[i]]<100:
print c[i],
ä¸é¢çä¸ä¸æ§è¡çææï¼
å¯ä»¥çå°ç¨åºå°ææä»äº3å100ä¹é´çå ç´ å¯¹åºçç´¢å¼å¼æäºåºæ¥ï¼å¦ææ³åæ¶è·å¾ç´¢å¼å¼åç¸åºçå ç´ ï¼åªéå°ä¸é¢çâprint c[i]â, æ¿æ¢ä¸º âprint c[i],arr[c[i]]âå³å¯ã å½ç¶è¿ç§æ¹æ³ä¹éç¨äºéæ©æ个ç¹å®å¼å¯¹åºçç´¢å¼ï¼ä¾å¦ææ³æ¾å°ææ3对åºçä½ç½®ï¼å¯ä»¥ç¨print b[arr==3]å³å¯æå°åºææå¼ä¸º3çå ç´ æ对åºçç´¢å¼ãå ¶å®ä¸è®ºæä¹åï¼é½æ¯ç¨å°äºæ°ç»è¿è¡å ³ç³»è¿ç®åçæå¸å°æ°ç»ï¼ç¶å让æ°ç»ä¸ä¸ºTrueçå°æ¹è¿è¡æ¾ç¤ºã
å¯ä»¥çå°ç¨åºå°ææä»äº3å100ä¹é´çå ç´ å¯¹åºçç´¢å¼å¼æäºåºæ¥ï¼å¦ææ³åæ¶è·å¾ç´¢å¼å¼åç¸åºçå ç´ ï¼åªéå°ä¸é¢çâprint c[i]â, æ¿æ¢ä¸º âprint c[i],arr[c[i]]âå³å¯ã
å½ç¶è¿ç§æ¹æ³ä¹éç¨äºéæ©æ个ç¹å®å¼å¯¹åºçç´¢å¼ï¼ä¾å¦ææ³æ¾å°ææ3对åºçä½ç½®ï¼å¯ä»¥ç¨print b[arr==3]å³å¯æå°åºææå¼ä¸º3çå ç´ æ对åºçç´¢å¼ãå ¶å®ä¸è®ºæä¹åï¼é½æ¯ç¨å°äºæ°ç»è¿è¡å ³ç³»è¿ç®åçæå¸å°æ°ç»ï¼ç¶å让æ°ç»ä¸ä¸ºTrueçå°æ¹è¿è¡æ¾ç¤ºã
å½ç¶è¿å¯ä»¥éè¿ä¸¤æ¬¡è¿è¡çéï¼åå«çéåºå¤§äº3çå ç´ åå°äº100çå ç´ å¯¹åºçç´¢å¼æ°ç»ï¼ç¶å两个æ°ç»å交éå¤çï¼å¨numpyä¸æä¸ä¸ªintersect1då½æ°å¯ä»¥è¿è¡è¿æ ·çè¿ç®ï¼ä½æ¯ä»ç¶éº»ç¦ãç®ååªè½æ³å°è¿äºåæ³ï¼ä¸ç¥éæåªä½é«æææ´å¥½çæ¹æ³ï¼æ¬¢è¿å¤§å®¶ä¸èµ·äº¤æµä¸ä¸ã