分享兴趣,传播快乐,增长见闻,留下美好!亲爱的您,这里是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新学苑原创,
如有侵权请联系沟通~
文案 | 邓粤慧
排版 | 邓粤慧
审核 | 百味