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

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

WBOY
WBOY轉載
2023-08-25 19:49:061716瀏覽

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刪除