slice python_Python slice()

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, 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函数使我们可以轻松地创建逐步的子序列,而无需对现有序列进行完整的迭代。

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,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对象没有单独使用,当与序列结合使用以创建子序列时很有用。

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,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

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, 2
my_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,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.

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

摘要 (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()是一个非常有用的函数。 我们可以轻松地根据步骤,开始和结束索引创建子序列,而无需进行完整的迭代。

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/23139/python-slice

slice python

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值