0

0

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

WBOY

WBOY

发布时间:2023-08-19 22:13:09

|

836人浏览过

|

来源于tutorialspoint

转载

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模块定义的整数数组为−

立即学习Python免费学习笔记(深入)”;

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.

    WeWedding婚纱影楼小程序
    WeWedding婚纱影楼小程序

    婚纱影楼小程序提供了一个连接用户与影楼的平台,相当于影楼在微信的官网。它能帮助影楼展示拍摄实力,记录访客数据,宣传优惠活动。使用频率高,方便传播,是影楼在微信端宣传营销的得力助手。功能特点:样片页是影楼展示优秀摄影样片提供给用户欣赏并且吸引客户的。套系页是影楼根据市场需求推出的不同套餐,用户可以按照自己的喜好预定套系。个人中心可以查看用户预约的拍摄计划,也可以获取到影楼的联系方式。

    下载
  • 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速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
高德地图升级方法汇总
高德地图升级方法汇总

本专题整合了高德地图升级相关教程,阅读专题下面的文章了解更多详细内容。

4

2026.01.16

全民K歌得高分教程大全
全民K歌得高分教程大全

本专题整合了全民K歌得高分技巧汇总,阅读专题下面的文章了解更多详细内容。

3

2026.01.16

C++ 单元测试与代码质量保障
C++ 单元测试与代码质量保障

本专题系统讲解 C++ 在单元测试与代码质量保障方面的实战方法,包括测试驱动开发理念、Google Test/Google Mock 的使用、测试用例设计、边界条件验证、持续集成中的自动化测试流程,以及常见代码质量问题的发现与修复。通过工程化示例,帮助开发者建立 可测试、可维护、高质量的 C++ 项目体系。

10

2026.01.16

java数据库连接教程大全
java数据库连接教程大全

本专题整合了java数据库连接相关教程,阅读专题下面的文章了解更多详细内容。

33

2026.01.15

Java音频处理教程汇总
Java音频处理教程汇总

本专题整合了java音频处理教程大全,阅读专题下面的文章了解更多详细内容。

15

2026.01.15

windows查看wifi密码教程大全
windows查看wifi密码教程大全

本专题整合了windows查看wifi密码教程大全,阅读专题下面的文章了解更多详细内容。

42

2026.01.15

浏览器缓存清理方法汇总
浏览器缓存清理方法汇总

本专题整合了浏览器缓存清理教程汇总,阅读专题下面的文章了解更多详细内容。

7

2026.01.15

ps图片相关教程汇总
ps图片相关教程汇总

本专题整合了ps图片设置相关教程合集,阅读专题下面的文章了解更多详细内容。

9

2026.01.15

ppt一键生成相关合集
ppt一键生成相关合集

本专题整合了ppt一键生成相关教程汇总,阅读专题下面的的文章了解更多详细内容。

6

2026.01.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 2.5万人学习

Django 教程
Django 教程

共28课时 | 3.2万人学习

SciPy 教程
SciPy 教程

共10课时 | 1.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号