您好,欢迎访问一九零五行业门户网

Python程序获取数组中给定数量的第一个项目

数组是一种具有相同数据类型的一组项目的数据结构,每个元素都由索引标识。
arrays in pythonpython 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.
slicingthe 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 arraylst = [1, 2, 0, 4, 1, 2, 3, 8] print (the original array is: , lst) print() numofitems = 4# get first number of elementsresult = lst[:numofitems]print (the first {} number of elements are: {}.format(numofitems, result))
outputthe 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 arraylst = [1, 2, 0] print (the original array is: , lst) print() numofitems = 4# get first number of elementsresult = lst[:numofitems]print (the first {} number of elements are: {}.format(numofitems, result))
outputthe 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 only.
使用numpy数组like list, we can also use numpy arrays to access the array elements.
example 的中文翻译为:示例在这个例子中,我们将尝试使用数组切片功能来访问numpy数组的前两个元素。
import numpy# creating arraynumpy_array = numpy.array([1, 3, 5, 6, 2, 9, 8])print (the original array is: , numpy_array) print() numofitems = 2# get first number of elementsresult = numpy_array[:numofitems]print (the result is: , result)
outputthe 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 modulearray模块是python内置模块,用于使用array()方法定义数组对象。
example 的中文翻译为:示例in this example, we will create an integer array using the array module
import array# creating arrayarr = array.array('i', [2, 1, 4, 3, 6, 5, 8, 7])print (the original array is: , arr) print() numofitems = 2# remove first elementsresult = arr[:numofitems]print (the result is: , result)
outputthe 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程序获取数组中给定数量的第一个项目的详细内容。
其它类似信息

推荐信息