陣列是相同資料類型的元素的集合,陣列中的每個元素都由索引值來識別。它是最簡單的資料結構,我們可以輕鬆新增或刪除元素。
Python 中的陣列
Python 沒有特定的資料結構來表示陣列。在這裡,我們可以使用列出陣列。
[9, 3, 1, 6, 9]
我們可以使用陣列或 NumPy 模組在 python 中處理陣列。
array('i', [1, 2, 3, 4])
上面的陣列是數組模組定義的整數陣列。
以同樣的方式,我們也可以使用 NumPy 模組定義 Numpy 陣列。
array([1, 2, 3, 4])
python中的索引是從0開始的。上面所有的陣列元素也是從0, 1,.., (n-1)開始索引的。
輸入輸出場景
假設我們有一個包含整數值的輸入陣列。結果陣列將附加一個元素。
Input array: A = [1, 5, 3, 6] Output array: [1, 5, 3, 6, 2]
整數元素 2 附加在給定數組的末尾。
在下面的文章中,我們看到了在 Python 中將元素追加到陣列中的多種方法。
使用列表資料結構
由於我們將 List 用作數組,因此可以使用 list.append() 方法將元素追加到數組中。
文法
list.append(element)
它將一個元素添加到列表的末尾。相當於 a[len(a):] = [x]。
範例
lst = [1, 2, 3, 4, 5, 6] print ("The original array is: ",lst) print() # append an element lst.append(9) print ("The resultant array is: ",lst)
輸出
The original array is: [1, 2, 3, 4, 5, 6] The resultant array is: [1, 2, 3, 4, 5, 6, 9]
元素 9 被追加到陣列中,並且被加入到陣列的末尾。
使用陣列模組
Python中的數組模組允許我們創建一個數組,並且可以緊湊地表示一個數組。要最初使用數組模組,我們需要導入數組模組。
文法
array.append(x)
將值為 x 的新項目附加到陣列末端。
範例
import array # creating array int_array = array.array('i', [1, 2, 3, 4]) print ("The original array is: ",int_array) print() # append an element int_array.append(0) print ("The resultant array is: ",int_array)
輸出
The original array is: array('i', [1, 2, 3, 4]) The resultant array is: array('i', [1, 2, 3, 4, 0])
int_array 物件在建立時指定了整數類型。如果我們嘗試將任何其他類型元素附加到數組對象,那麼它將引發如下錯誤。
TypeError - 需要整數參數,但得到了浮點數
使用 NumPy 模組
透過使用 numpy 函式庫,我們可以使用 numpy.array() 方法輕鬆建立陣列。同樣,我們也可以使用 numpy.append() 方法向陣列追加一個元素。
文法
numpy.append(array, element)
該方法將一個元素追加到陣列的末端。它會建立一個新數組,該數組可以是舊數組的副本,並附加元素,以便原始數組保持不變。
範例
在此範例中,我們將使用 for 迴圈迭代字串陣列元素。
import numpy # creating array array = numpy.array([1, 2, 3, 4]) print ("The original array is: ", array) print() # append an element result = numpy.append(array, 9) print ("The resultant array is: ", result)
輸出
The original array is: [1 2 3 4] The resultant array is: [1 2 3 4 9]
這裡原始陣列保持不變,結果陣列已使用新元素更新。
以上是Python程式將一個元素加入到陣列中的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Python是解釋型語言,但也包含編譯過程。 1)Python代碼先編譯成字節碼。 2)字節碼由Python虛擬機解釋執行。 3)這種混合機制使Python既靈活又高效,但執行速度不如完全編譯型語言。

UseeAforloopWheniteratingOveraseQuenceOrforAspecificnumberoftimes; useAwhiLeLoopWhenconTinuingUntilAcIntiment.forloopsareIdealForkNownsences,而WhileLeleLeleLeleLeleLoopSituationSituationsItuationsItuationSuationSituationswithUndEtermentersitations。

pythonloopscanleadtoerrorslikeinfiniteloops,modifyingListsDuringteritation,逐個偏置,零indexingissues,andnestedloopineflinefficiencies

forloopsareadvantageousforknowniterations and sequests,供應模擬性和可讀性;而LileLoopSareIdealFordyNamicConcitionSandunknowniterations,提供ControloperRoverTermination.1)forloopsareperfectForeTectForeTerToratingOrtratingRiteratingOrtratingRitterlistlistslists,callings conspass,calplace,cal,ofstrings ofstrings,orstrings,orstrings,orstrings ofcces

pythonisehybridmodeLofCompilation和interpretation:1)thepythoninterpretercompilesourcecececodeintoplatform- interpententbybytecode.2)thepythonvirtualmachine(pvm)thenexecutecutestestestestestesthisbytecode,ballancingEaseofuseEfuseWithPerformance。

pythonisbothinterpretedAndCompiled.1)它的compiledTobyTecodeForportabilityAcrosplatforms.2)bytecodeisthenInterpreted,允許fordingfordforderynamictynamictymictymictymictyandrapiddefupment,儘管Ititmaybeslowerthananeflowerthanancompiledcompiledlanguages。

在您的知識之際,而foroopsareideal insinAdvance中,而WhileLoopSareBetterForsituations則youneedtoloopuntilaconditionismet

ForboopSareSusedwhenthentheneMberofiterationsiskNownInAdvance,而WhileLoopSareSareDestrationsDepportonAcondition.1)ForloopSareIdealForiteratingOverSequencesLikelistSorarrays.2)whileLeleLooleSuitableApeableableableableableableforscenarioscenarioswhereTheLeTheLeTheLeTeLoopContinusunuesuntilaspecificiccificcificCondond


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境

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

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。