数组是一种具有相同数据类型的一组项目的数据结构,每个元素都由索引标识。
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 try 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 only.
使用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中文网其他相关文章!

numpyArraysareAreBetterFornumericalialoperations andmulti-demensionaldata,而learthearrayModuleSutableforbasic,内存效率段

numpyArraySareAreBetterForHeAvyNumericalComputing,而lelethearRayModulesiutable-usemoblemory-connerage-inderabledsswithSimpleDatateTypes.1)NumpyArsofferVerverVerverVerverVersAtility andPerformanceForlargedForlargedAtatasetSetsAtsAndAtasEndCompleXoper.2)

ctypesallowscreatingingangandmanipulatingc-stylarraysinpython.1)usectypestoInterfacewithClibrariesForperfermance.2)createc-stylec-stylec-stylarraysfornumericalcomputations.3)passarraystocfunctions foreforfunctionsforeffortions.however.however,However,HoweverofiousofmemoryManageManiverage,Pressiveo,Pressivero

Inpython,一个“列表” isaversatile,mutableSequencethatCanholdMixedDatateTypes,而“阵列” isamorememory-效率,均质sepersequeSequeSequeReDencErequiringElements.1)

pythonlistsandArraysareBothable.1)列表Sareflexibleandsupportereceneousdatabutarelessmory-Memory-Empefficity.2)ArraysareMoremoremoremoreMemoremorememorememorememoremorememogeneSdatabutlesserversEversementime,defteringcorcttypecrecttypececeDepeceDyusagetoagetoavoavoiDerrors。

Python和C 各有优势,选择应基于项目需求。1)Python适合快速开发和数据处理,因其简洁语法和动态类型。2)C 适用于高性能和系统编程,因其静态类型和手动内存管理。

选择Python还是C 取决于项目需求:1)如果需要快速开发、数据处理和原型设计,选择Python;2)如果需要高性能、低延迟和接近硬件的控制,选择C 。

通过每天投入2小时的Python学习,可以有效提升编程技能。1.学习新知识:阅读文档或观看教程。2.实践:编写代码和完成练习。3.复习:巩固所学内容。4.项目实践:应用所学于实际项目中。这样的结构化学习计划能帮助你系统掌握Python并实现职业目标。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

Dreamweaver Mac版
视觉化网页开发工具

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

Atom编辑器mac版下载
最流行的的开源编辑器

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。