博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
slice python_Python slice()
阅读量:2558 次
发布时间:2019-05-11

本文共 3857 字,大约阅读时间需要 12 分钟。

slice python

Python slice() function returns a slice object representing the set of indices specified by range(start, stop, step).

Python slice()函数返回一个slice对象,该对象代表由range(start,stop,step)指定的一组索引。

Python slice() (Python slice())

Python slice function syntax is:

Python slice函数的语法为:

class slice(stop)class slice(start, stop[, step])

Note that slice function returns slice object. We can pass this object as a set of indices with sequences such as string, , etc.

请注意,slice函数返回slice对象。 我们可以将该对象作为一组带有字符串, , 等序列的索引传递。

Python slice function allows us to create a stepwise sub-sequence easily without doing complete iteration on the existing sequence.

Python slice函数使我们可以轻松地创建逐步的子序列,而无需对现有序列进行完整的迭代。

slice()函数参数 (slice() function arguments)

  • start: specifies the start of the index value. It’s optional and defaults to None.

    start :指定索引值的开始。 它是可选的,默认为无。
  • stop: specifies the end of the index value. This is a mandatory parameter.

    stop :指定索引值的结尾。 这是必填参数。
  • step: specifies the steps to take from start to stop index. It’s an optional parameter and defaults to None.

    step :指定从开始到停止索引要采取的步骤。 这是一个可选参数,默认为无。

Python slice object has read-only data attributes – start, stop and step – which return the argument values (or default value).

Python slice对象具有只读数据属性(开始,停止和步进),这些属性返回参数值(或默认值)。

Let’s see how to create slice objects.

让我们看看如何创建切片对象。

s = slice(1, 10, 2)  # indexes 1,3,5,7,9print(type(s))print(s.start)print(s.stop)print(s.step)s = slice(5)  # indexes 0,1,2,3,4print(s.start)print(s.stop)print(s.step)

Output:

输出:

1102None5None

Python slice object has no use on its own, it’s useful when used in conjunction with sequences to create a sub-sequence.

Python slice对象没有单独使用,当与序列结合使用以创建子序列时很有用。

Python切片字符串 (Python slice string)

Let’s see how to use slice function with string. We will pass slice object just like a normal index to get the substring value from a string.

让我们看看如何对字符串使用slice函数。 我们将像普通索引一样传递slice对象,以从字符串获取子字符串值。

s = slice(1, 10, 2)  # indexes 1,3,5,7,9print('abcde'[s])

Output: bd

输出: bd

Note that if the slice indexes are more than the length of the sequence, no exception is raised and data is returned till the maximum available index.

请注意,如果切片索引大于序列的长度,则不会引发异常,并且将返回数据,直到最大可用索引为止。

We can also pass negative values for slice() function. In that case, the iteration will be performed backward i.e. from end to start.

我们还可以为slice()函数传递负值。 在这种情况下,迭代将向后执行,即从头到尾。

s = slice(-1, -3, -1)  # indexes -1, -2print('abcde'[s])

Output: ed

输出: ed

Python切片列表/数组 (Python slice list/array)

Let’s look at an example of using slice() function with list or array.

我们来看一个将slice()函数与列表或数组一起使用的示例。

s = slice(0, 3)  # indexes 0, 1, 2my_list = [1, 2, 3, 4, 5, 6]print(my_list[s])

Output: [1, 2, 3]

输出: [1, 2, 3]

Python切片元组 (Python slice tuple)

We can use slicing with tuple too because it’s a sequence.

我们也可以对元组使用切片,因为这是一个序列。

s = slice(2)my_tuple = [1, 2, 3, 4, 5, 6]print(my_tuple[s])

Output: [1, 2]

输出: [1, 2]

Python slice扩展索引语法 (Python slice extended indexing syntax)

Since slicing is very popular in numerical python, there is a shorthand way to create a slice object.

由于切片在数值python中非常流行,因此有一种创建切片对象的简便方法。

a[start:stop:step]

Let’s see some examples of slicing using the shorthand approach.

让我们来看一些使用速记方法进行切片的示例。

x = 'abcde'y = x[1:3:1]  # 1,2print(y)y = x[1:3]  # 1,2print(y)y = x[2:]  # 2 to length of sequenceprint(y)y = x[:5:2]  # 0,2,4print(y)y = x[:]  # copy of sequenceprint(y)y = x[-1:-4:-1]  # reverse iteration, end to startprint(y)

Output:

输出:

bcbccdeaceabcdeedc

The output is self-explanatory and important details are already mentioned in the comments.

输出是不言自明的,注释中已经提到了重要的细节。

摘要 (Summary)

Python slice() is a very useful function. We can create sub-sequence based on steps, start and end indexes easily without doing complete iteration.

Python slice()是一个非常有用的函数。 我们可以轻松地根据步骤,开始和结束索引创建子序列,而无需进行完整的迭代。

. 检出完整的python脚本和更多Python示例。

Reference:

参考:

翻译自:

slice python

转载地址:http://fxmzd.baihongyu.com/

你可能感兴趣的文章
BiTree
查看>>
5个基于HTML5的加载动画推荐
查看>>
水平权限漏洞的修复方案
查看>>
静态链接与动态链接的区别
查看>>
Android 关于悬浮窗权限的问题
查看>>
如何使用mysql
查看>>
linux下wc命令详解
查看>>
敏捷开发中软件测试团队的职责和产出是什么?
查看>>
在mvc3中使用ffmpeg对上传视频进行截图和转换格式
查看>>
python的字符串内建函数
查看>>
Spring - DI
查看>>
微软自己的官网介绍 SSL 参数相关
查看>>
Composite UI Application Block (CAB) 概念和术语
查看>>
ajax跨域,携带cookie
查看>>
阶段3 2.Spring_01.Spring框架简介_03.spring概述
查看>>
阶段3 2.Spring_02.程序间耦合_1 编写jdbc的工程代码用于分析程序的耦合
查看>>
阶段3 2.Spring_01.Spring框架简介_04.spring发展历程
查看>>
阶段3 2.Spring_02.程序间耦合_3 程序的耦合和解耦的思路分析1
查看>>
阶段3 2.Spring_02.程序间耦合_5 编写工厂类和配置文件
查看>>
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>