陣列是具有相同資料類型的一組項目的資料結構,每個元素都由索引標識。
Arrays in python
Python does not have its own data structure to represent an array. However, we can use the list data structure as an alternative to the arrays. Here we will use list an array −
[10, 4, 11, 76, 99]
Python也提供了一些更合適的模組,這些模組是Numpy和array模組。
使用array模組定義的整數陣列為−
array('i', [1, 2, 3, 4])
A Numpy array defined by the NumPy module is −
array([1, 2, 3, 4])
在本文中,我們將看到如何從陣列中取得給定數量的第一項。
輸入輸出場景
假設我們有一個包含9個整數值的輸入陣列。在輸出中,前幾個項目是根據指定的數字存取的。
Input array: [1, 2, 3, 4, 5, 6, 7, 8, 9] Output: [1, 2, 3]
前三個項目1、2、3從輸入陣列存取。讓我們來看一個包含所有字串元素的陣列。
Input array: [‘a’, ‘b’, ‘c’, ‘d’, ‘e’] Output: [‘a’, ‘b’]
First 2items are retrieved from the input array. In the below examples, we will mainly use python slicing features to retrieve the first few elements.
Slicing
The slicing is used to access the group of elements from a sequence. Following is the syntax to perform slicing –
sequence_object[start : end]
Where,
Start − The starting index where the slicing of the iterable starts. By default, it is 0.
End − The ending index where the slicing of the iterable stops. The default value is the length of the iterable object. And this value is excluded.
使用列表
We can use the list-slicing feature to access the first given number of items from an array.
Example
的中文翻譯為:範例
讓我們舉一個例子,並使用列表切片來存取前面給定數量的元素。
# creating array lst = [1, 2, 0, 4, 1, 2, 3, 8] print ("The original array is: ", lst) print() numOfItems = 4 # Get first number of elements result = lst[:numOfItems] print ("The first {} number of elements are: {}".format(numOfItems, result))
Output
#The original array is: [1, 2, 0, 4, 1, 2, 3, 8] The first 4 number of elements are: [1, 2, 0, 4]
The first 4 elements are accessed from the given array using the lst[:numOfItems] syntax and those elements are stored in the result variable.
Example
的中文翻譯為:範例
In this example, we will try to access the exceeded number of elements from an array.
# creating array lst = [1, 2, 0] print ("The original array is: ", lst) print() numOfItems = 4 # Get first number of elements result = lst[:numOfItems] print ("The first {} number of elements are: {}".format(numOfItems, result))
Output
#The original array is: [1, 2, 0] The first 4 number of elements are: [1, 2, 0]
The requested number of items are more compared to the total number of items available in the array list. Instead of rising an error, the slicing object lst[:numOfItems] displayed the available #elements on. lst[:numOfItems] displayed the available #elements.
使用NumPy陣列Like List, we can also use NumPy arrays to access the array elements.
Example
的中文翻譯為:
範例在這個範例中,我們將嘗試使用陣列切片功能來存取NumPy陣列的前兩個元素。
import numpy # creating array numpy_array = numpy.array([1, 3, 5, 6, 2, 9, 8]) print ("The original array is: ", numpy_array) print() numOfItems = 2 # get first number of elements result = numpy_array[:numOfItems] print ("The result is: ", result)Output
#
The original array is: [1 3 5 6 2 9 8]
The result is: [1 3]
The first two elements 1,3 are accessed from the numpy array object.Using array module
array模組是Python內建模組,用於使用array()方法定義陣列物件。
Example
的中文翻譯為:
範例In this example, we will create an integer array using the array module
import array # creating array arr = array.array('i', [2, 1, 4, 3, 6, 5, 8, 7]) print ("The original array is: ", arr) print() numOfItems = 2 # remove first elements result = arr[:numOfItems] print ("The result is: ", result)Output
#
The original array is: array('i', [2, 1, 4, 3, 6, 5, 8, 7])
The result is: array('i', [2, 1])
The first 2 elements from the input array arr are stored in the result variable.以上是Python程式取得數組中給定數量的第一個項目的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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

Python和C 在内存管理和控制方面的差异显著。1.Python使用自动内存管理,基于引用计数和垃圾回收,简化了程序员的工作。2.C 则要求手动管理内存,提供更多控制权但增加了复杂性和出错风险。选择哪种语言应基于项目需求和团队技术栈。

Python在科學計算中的應用包括數據分析、機器學習、數值模擬和可視化。 1.Numpy提供高效的多維數組和數學函數。 2.SciPy擴展Numpy功能,提供優化和線性代數工具。 3.Pandas用於數據處理和分析。 4.Matplotlib用於生成各種圖表和可視化結果。

選擇Python還是C 取決於項目需求:1)Python適合快速開發、數據科學和腳本編寫,因其簡潔語法和豐富庫;2)C 適用於需要高性能和底層控制的場景,如係統編程和遊戲開發,因其編譯型和手動內存管理。

Python在數據科學和機器學習中的應用廣泛,主要依賴於其簡潔性和強大的庫生態系統。 1)Pandas用於數據處理和分析,2)Numpy提供高效的數值計算,3)Scikit-learn用於機器學習模型構建和優化,這些庫讓Python成為數據科學和機器學習的理想工具。

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

Python在Web開發中的關鍵應用包括使用Django和Flask框架、API開發、數據分析與可視化、機器學習與AI、以及性能優化。 1.Django和Flask框架:Django適合快速開發複雜應用,Flask適用於小型或高度自定義項目。 2.API開發:使用Flask或DjangoRESTFramework構建RESTfulAPI。 3.數據分析與可視化:利用Python處理數據並通過Web界面展示。 4.機器學習與AI:Python用於構建智能Web應用。 5.性能優化:通過異步編程、緩存和代碼優

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


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

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

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具