首頁  >  文章  >  後端開發  >  Python程式取得數組中給定數量的第一個項目

Python程式取得數組中給定數量的第一個項目

WBOY
WBOY轉載
2023-08-19 22:13:09772瀏覽

Python程式取得數組中給定數量的第一個項目

陣列是具有相同資料類型的一組項目的資料結構,每個元素都由索引標識。

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 tr​​y 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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除