首頁 > 後端開發 > Python教學 > Python程式刪除數組中的第一個元素

Python程式刪除數組中的第一個元素

WBOY
發布: 2023-08-25 19:49:06
轉載
1806 人瀏覽過

Python程式刪除數組中的第一個元素

In order to remove the first element of the array, the index that must be considered is 0 as the index of the first element in any array is always 0. As like removing the last element from an array, removing the first element from the array can be processed using the same techniques.

讓我們將這些技術應用於刪除陣列的第一個元素。現在,我們將逐一討論用於從陣列中刪除第一個元素的方法和關鍵字。

使用pop()方法

The method pop() is used to delete the elements of the arrays, lists, etc in Python programming language. This mechanism works by using the index of the element which must be removed or deleted from the array.

##所以,要刪除陣列的第一個元素,請考慮索引0。元素只是從數組中彈出並被刪除。下面描述了「pop()」方法的語法。讓我們使用這個方法刪除陣列的第一個元素。

文法

arr.pop(0)
登入後複製

Example

In this example, we are going to discuss about the process of removing the first element of an array by using pop() method. The steps followed to construct such program are as follows −

  • 宣告一個陣列並在陣列中定義一些元素。

  • 透過使用pop()方法,在方法的括號內提及陣列的第一個索引,即0,以刪除第一個元素。

  • 刪除第一個元素後列印陣列。

  • arr = [" Hello ", " Programming ", " Python ", " World ", " Delete ", " Element "]
    first_index = 0
    
    print(" The elements of the array before deletion: ")
    print(arr)
    
    print(" The elements of the array after deletion: ")
    arr.pop(first_index)
    print(arr)
    
    登入後複製
Output

#上述程式的輸出如下:

The elements of the array before deletion: 
[' Hello ', ' Programming ', ' Python ', ' World ', ' Delete ', ' Element ']
The elements of the array after deletion:
[' Programming ', ' Python ', ' World ', ' Delete ', ' Element ']
登入後複製
登入後複製
登入後複製

Using the del Keyword

The keyword del is used to delete the objects in Python. This keyword is also used in order to delete the last element of an array or any element by using its index. So, we use this keyific to delete by using its index. So, we use this keyific to delete specor sspecor s片 this keyificor meect this this. in Python. Following is the syntax of this keyword −

#

del arr[first_index]
登入後複製

Example

在下面的範例中,我們將討論使用「 del 」關鍵字刪除陣列的第一個元素的過程。

arr = [" Hello ", " Programming ", " Python ", " World ", " Delete ", " Element "]
first_index = 0
print(" The elements of the array before deletion: ")
print(arr)

print(" The elements of the array after deletion: ")
del arr[first_index]
print(arr)
登入後複製

Output

#上述程式的輸出如下:

The elements of the array before deletion: 
[' Hello ', ' Programming ', ' Python ', ' World ', ' Delete ', ' Element ']
The elements of the array after deletion:
[' Programming ', ' Python ', ' World ', ' Delete ', ' Element ']
登入後複製
登入後複製
登入後複製

Using Delete() Method of Numpy Module

The method delete() can remove the element from the array when its index is clearly mentioned. In order to use the method delete(), the array should be converted into the form of Numpy Array. The conversion of normal array to a numpy array can also be performed by using the module. The syntax of the delete() method is described below.

文法

variable = n.delete(arr, first_index)
登入後複製

Example

In this example, we are going to discuss about the process of removing the first element of an array by using delete() method of Numpy module.

import numpy as n
arr = [" Hello ", " Programming ", " Python ", " World ", " Delete ", " Element "]
variable = n.array(arr)
first_index = 0
print(" The elements of the array before deletion: ")
print(variable)
variable = n.delete(arr, first_index)
print(" The elements of the array after deletion: ")
print(variable)
登入後複製

Output

#上述程式的輸出如下:

The elements of the array before deletion: 
[' Hello ', ' Programming ', ' Python ', ' World ', ' Delete ', ' Element ']
The elements of the array after deletion:
[' Programming ', ' Python ', ' World ', ' Delete ', ' Element ']
登入後複製
登入後複製
登入後複製
結論

我們可以清楚地觀察到,這三個程式的輸出是相同的,這告訴我們透過使用這三種方式,成功地從陣列中刪除了第一個元素。透過使用簡單的技術,可以非常容易地刪除數組中任意索引位置的元素。如果使用者知道數組元素的索引,那麼刪除過程將變得非常簡單。如果不知道索引,至少必須知道元素的值,以便可以應用「remove()」方法。

以上是Python程式刪除數組中的第一個元素的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板