赞
踩
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 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, list, tuple 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函数使我们可以轻松地创建逐步的子序列,而无需对现有序列进行完整的迭代。
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,9
- print(type(s))
- print(s.start)
- print(s.stop)
- print(s.step)
-
- s = slice(5) # indexes 0,1,2,3,4
- print(s.start)
- print(s.stop)
- print(s.step)
Output:
输出:
-
-
-
-
-
- 1
- 10
- 2
- None
- 5
- None
-
-
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对象没有单独使用,当与序列结合使用以创建子序列时很有用。
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,9
- print('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, -2
- print('abcde'[s])
Output: ed
输出: ed
Let’s look at an example of using slice() function with list or array.
我们来看一个将slice()函数与列表或数组一起使用的示例。
- s = slice(0, 3) # indexes 0, 1, 2
- my_list = [1, 2, 3, 4, 5, 6]
- print(my_list[s])
Output: [1, 2, 3]
输出: [1, 2, 3]
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]
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,2
- print(y)
-
- y = x[1:3] # 1,2
- print(y)
-
- y = x[2:] # 2 to length of sequence
- print(y)
-
- y = x[:5:2] # 0,2,4
- print(y)
-
- y = x[:] # copy of sequence
- print(y)
-
- y = x[-1:-4:-1] # reverse iteration, end to start
- print(y)
Output:
输出:
- bc
- bc
- cde
- ace
- abcde
- edc
The output is self-explanatory and important details are already mentioned in the comments.
输出是不言自明的,注释中已经提到了重要的细节。
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()是一个非常有用的函数。 我们可以轻松地根据步骤,开始和结束索引创建子序列,而无需进行完整的迭代。
Reference: Official Documentation
参考: 官方文档
slice python
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。