百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

浅学python | 列表切片

bigegpt 2024-09-10 11:17 4 浏览

分享兴趣,传播快乐,增长见闻,留下美好!亲爱的您,这里是LearningYard学苑。欢迎大家继续访问学苑内容,今天小编为大家带来有关Python的知识。

Share interest, spread happiness, increase knowledge, leave a good! Dear you, here is the LearningYard Academy. Welcome to continue to visit the content of the academy, today xiaobian to bring you knowledge about Python management


切片操作

切片适用于列表、元组、字符串、range对象等类型,但作用于列表时功能最强大。可以使用切片来截取列表中的任何部分,得到一个新列表,也可以通过切片来修改和删除列表中部分元素,甚至可以通过切片操作为列表对象增加元素。
切片使用2个冒号分隔的3个数字来完成:
第一个数字表示切片开始位置(默认为0)。
第二个数字表示切片截止(但不包含)位置(默认为列表长度)。
第三个数字表示切片的步长(默认为1),当步长省略时可以顺便省略最后一个冒号。
切片操作不会因为下标越界而抛出异常,而是简单地在列表尾部截断或者返回一个空列表,代码具有更强的健壮性。

Slices are suitable for lists, tuples, strings, range objects, etc., but they are the most powerful when working on a list. You can use slices to intercept any part of the list, get a new list, or modify and delete parts of the list, or even add elements to the list object.

The slice is done using three numbers separated by two colons:

The first number indicates the slice start location (the default is 0).

The second number represents the slice cutoff (but not included) location (the default is the list length).

The third number represents the step length of the slice (the default is 1), and the last colon can be incidentally omitted when the step length is omitted.

The slice operation does not throw exceptions because the subscript overbounds, but simply truncates at the end of the list or returns an empty list and the code is more robust.

可以使用切片来原地修改列表内容


常用序列的常用序列函数

1)len(列表):返回列表中的元素个数,同样适用于元组、字典、集合、字符串等。
2)max(列表)、 min(列表):返回列表中的最大或最小元素,同样适用于元组、字典、集合、range对象等。
3)sum(列表):对列表的元素进行求和运算,对非数值型列表运算需要指定start参数,同样适用于元组、range。

1) The len (list): Returns the number of elements in the list, also suitable for tuples, dictionaries, collections, strings, etc.

2) The max (list), min (list): returns the maximum or minimum element in the list, which also applies to tuples, dictionaries, collections, range objects, etc.

3) The sum (list): sum up the elements of the list, and specify the start parameter for the non-numerical list operation, also applicable to tuples, range.

今天的分享就到这儿了

如果您对今天的文章有独特的想法

欢迎给我们留言

让我们相约明天

祝您今天过得开心快乐!

That's all for today's sharing

If you have unique ideas about today's article

Feel free to leave us a comment

Let's meet for tomorrow

I wish you a happy life today!

翻译:金山词霸

本文由Learning Yard新学苑原创,

如有侵权请联系沟通~


文案 | 邓粤慧

排版 | 邓粤慧

审核 | 百味

相关推荐

Go语言泛型-泛型约束与实践(go1.7泛型)

来源:械说在Go语言中,Go泛型-泛型约束与实践部分主要探讨如何定义和使用泛型约束(Constraints),以及如何在实际开发中利用泛型进行更灵活的编程。以下是详细内容:一、什么是泛型约束?**泛型...

golang总结(golang实战教程)

基础部分Go语言有哪些优势?1简单易学:语法简洁,减少了代码的冗余。高效并发:内置强大的goroutine和channel,使并发编程更加高效且易于管理。内存管理:拥有自动垃圾回收机制,减少内...

Go 官宣:新版 Protobuf API(go pro版本)

原文作者:JoeTsai,DamienNeil和HerbieOng原文链接:https://blog.golang.org/a-new-go-api-for-protocol-buffer...

Golang开发的一些注意事项(一)(golang入门项目)

1.channel关闭后读的问题当channel关闭之后再去读取它,虽然不会引发panic,但会直接得到零值,而且ok的值为false。packagemainimport"...

golang 托盘菜单应用及打开系统默认浏览器

之前看到一个应用,用go语言编写,说是某某程序的windows图形化客户端,体验一下发现只是一个托盘,然后托盘菜单的控制面板功能直接打开本地浏览器访问程序启动的webserver网页完成gui相关功...

golang标准库每日一库之 io/ioutil

一、核心函数概览函数作用描述替代方案(Go1.16+)ioutil.ReadFile(filename)一次性读取整个文件内容(返回[]byte)os.ReadFileioutil.WriteFi...

文件类型更改器——GoLang 中的 CLI 工具

我是如何为一项琐碎的工作任务创建一个简单的工具的,你也可以上周我开始玩GoLang,它是一种由Google制作的类C编译语言,非常轻量和快速,事实上它经常在Techempower的基准测...

Go (Golang) 中的 Channels 简介(golang channel长度和容量)

这篇文章重点介绍Channels(通道)在Go中的工作方式,以及如何在代码中使用它们。在Go中,Channels是一种编程结构,它允许我们在代码的不同部分之间移动数据,通常来自不同的goro...

Golang引入泛型:Go将Interface「」替换为“Any”

现在Go将拥有泛型:Go将Interface{}替换为“Any”,这是一个类型别名:typeany=interface{}这会引入了泛型作好准备,实际上,带有泛型的Go1.18Beta...

一文带你看懂Golang最新特性(golang2.0特性)

作者:腾讯PCG代码委员会经过十余年的迭代,Go语言逐渐成为云计算时代主流的编程语言。下到云计算基础设施,上到微服务,越来越多的流行产品使用Go语言编写。可见其影响力已经非常强大。一、Go语言发展历史...

Go 每日一库之 java 转 go 遇到 Apollo?让 agollo 来平滑迁移

以下文章来源于GoOfficialBlog,作者GoOfficialBlogIntroductionagollo是Apollo的Golang客户端Apollo(阿波罗)是携程框架部门研...

Golang使用grpc详解(golang gcc)

gRPC是Google开源的一种高性能、跨语言的远程过程调用(RPC)框架,它使用ProtocolBuffers作为序列化工具,支持多种编程语言,如C++,Java,Python,Go等。gR...

Etcd服务注册与发现封装实现--golang

服务注册register.gopackageregisterimport("fmt""time"etcd3"github.com/cor...

Golang:将日志以Json格式输出到Kafka

在上一篇文章中我实现了一个支持Debug、Info、Error等多个级别的日志库,并将日志写到了磁盘文件中,代码比较简单,适合练手。有兴趣的可以通过这个链接前往:https://github.com/...

如何从 PHP 过渡到 Golang?(php转golang)

我是PHP开发者,转Go两个月了吧,记录一下使用Golang怎么一步步开发新项目。本着有坑填坑,有错改错的宗旨,从零开始,开始学习。因为我司没有专门的Golang大牛,所以我也只能一步步自己去...