Python程式用於在陣列中尋找指定項目的第一次出現的索引
陣列是一種資料結構,用於依序儲存相同資料類型的元素。並且儲存的元素由索引值來標識。 Python 沒有特定的資料結構來表示陣列。但是,我們可以使用 List 資料結構或 Numpy 模組來處理陣列。
在本文中,我們看到了多種取得指定項目在陣列中第一次出現的索引的方法。
輸入輸出場景
現在讓我們來看看一些輸入輸出場景。
假設我們有一個包含很少元素的輸入陣列。在輸出中,我們將取得第一次出現的指定值的索引。
Input array: [1, 3, 9, 4, 1, 7] specified value = 9 Output: 2
指定的元素 9 僅在陣列中出現一次,該值的結果索引為 2。
Input array: [1, 3, 6, 2, 4, 6] specified value = 6 Output: 2
給定元素 6 在陣列中出現了兩次,第一次出現的索引值為 2。
使用list.index()方法
list.index() 方法可協助您尋找陣列中給定元素第一次出現的索引。如果清單中存在重複元素,則傳回該元素的第一個索引。以下是語法 -
list.index(element, start, end)
第一個參數是我們想要取得索引的元素,第二個和第三個參數是可選參數,從哪裡開始和結束對給定元素的搜尋。
list.index() 方法傳回一個整數值,它是我們傳遞給該方法的給定元素的索引。
範例
在上面的範例中,我們將使用index()方法。
# creating array arr = [1, 3, 6, 2, 4, 6] print ("The original array is: ", arr) print() specified_item = 6 # Get index of the first occurrence of the specified item item_index = arr.index(specified_item) print('The index of the first occurrence of the specified item is:',item_index)
輸出
The original array is: [1, 3, 6, 2, 4, 6] The index of the first occurrence of the specified item is: 2
給定值 6 在陣列中出現兩次,但 index() 方法僅傳回第一次出現值的索引。
使用for迴圈
類似地,我們可以使用 for 迴圈和 if 條件來取得出現在陣列第一個位置的指定項目的索引。
範例
在這裡,我們將使用 for 迴圈迭代數組元素。
# creating array arr = [7, 3, 1, 2, 4, 3, 8, 5, 4] print ("The original array is: ", arr) print() specified_item = 4 # Get the index of the first occurrence of the specified item for index in range(len(arr)): if arr[index] == specified_item: print('The index of the first occurrence of the specified item is:',index) break
輸出
The original array is: [7, 3, 1, 2, 4, 3, 8, 5, 4] The index of the first occurrence of the specified item is: 4
給定值 4 在陣列中重複出現,但上面的範例只傳回第一個出現的值的索引。
使用 numpy.where()
numpy.where() 方法用於根據給定條件過濾陣列元素。透過使用這個方法,我們可以獲得給定元素的索引。以下是語法 -
numpy.where(condition, [x, y, ]/)
範例
在此範例中,我們將使用帶有條件的 numpy.where() 方法。
import numpy as np # creating array arr = np.array([2, 4, 6, 8, 1, 3, 9, 6]) print("Original array: ", arr) specified_index = 6 index = np.where(arr == specified_index) # Get index of the first occurrence of the specified item print('The index of the first occurrence of the specified item is:',index[0][0])
輸出
Original array: [2 4 6 8 1 3 9 6] The index of the first occurrence of the specified item is: 2
條件arr ==指定索引檢查numpy陣列中的給定元素,並傳回一個包含滿足給定條件或True的元素的陣列。從結果陣列中,我們可以使用索引[0][0]來取得第一次出現的索引。
以上是Python程式用於在陣列中尋找指定項目的第一次出現的索引的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Python适合数据科学、Web开发和自动化任务,而C 适用于系统编程、游戏开发和嵌入式系统。Python以简洁和强大的生态系统著称,C 则以高性能和底层控制能力闻名。

Python在遊戲和GUI開發中表現出色。 1)遊戲開發使用Pygame,提供繪圖、音頻等功能,適合創建2D遊戲。 2)GUI開發可選擇Tkinter或PyQt,Tkinter簡單易用,PyQt功能豐富,適合專業開發。

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。

要在有限的時間內最大化學習Python的效率,可以使用Python的datetime、time和schedule模塊。 1.datetime模塊用於記錄和規劃學習時間。 2.time模塊幫助設置學習和休息時間。 3.schedule模塊自動化安排每週學習任務。

Python在開發效率上優於C ,但C 在執行性能上更高。 1.Python的簡潔語法和豐富庫提高開發效率。 2.C 的編譯型特性和硬件控制提升執行性能。選擇時需根據項目需求權衡開發速度與執行效率。

Python在自動化、腳本編寫和任務管理中表現出色。 1)自動化:通過標準庫如os、shutil實現文件備份。 2)腳本編寫:使用psutil庫監控系統資源。 3)任務管理:利用schedule庫調度任務。 Python的易用性和豐富庫支持使其在這些領域中成為首選工具。

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

每天學習Python兩個小時是否足夠?這取決於你的目標和學習方法。 1)制定清晰的學習計劃,2)選擇合適的學習資源和方法,3)動手實踐和復習鞏固,可以在這段時間內逐步掌握Python的基本知識和高級功能。
