python中切片_Python中的切片符号

python中切片

Python符号 (Python notation)

The slice() functions returns a slice object. The slice() object is used to slice a given sequence (string, bytes, tuple, list or range) or any object which supports sequence protocol. Slicing is used to retrieve the subset of values. The basic slicing technique is to define a starting point.

slice()函数返回一个slice对象。 slice()对象用于切片给定的序列(字符串,字节,元组,列表或范围)或任何支持序列协议的对象。 切片用于检索值的子集。 基本切片技术是定义起点。

Slice is a way to extract certain elements from data types like string and lists.

切片是一种从字符串和列表之类的数据类型中提取某些元素的方法。

Syntax:

句法:

    slice[stop]
    slice[start:stop:step]

Other variations:

其他变化:

    slice[start:stop]
    slice[start:]
    slice[:stop]
    slice[:]
    slice[start:stop:step]

Parameter(s):

参数:

  • start: starting integer where the slicing of the object starts to.

    start :对象切片开始的起始整数。

  • stop: integer until which the slicing takes place. The slicing stops at index stop -1.

    停止 :整数,直到切片发生。 切片在索引停止-1处停止。

  • step: integer value which determines the increment between each index for slicing.

    step :整数值,用于确定切片时每个索引之间的增量。

  • Note: If a single parameter is passed, start and step is None.

    注意:如果传递单个参数,则start和step为None。

Example:

例:

-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> list = [1,2,3,4,5,6]
>>> print(list[3:5])
[4, 5] # here the elements from index 3 through 5 are extracted and added to list
>>> print(list[2:5:2])
[3, 5] # here the elements from index 2 through 5 with a step of 2 is extracted (every 2nd value)
>>> print(list[1:])
[2, 3, 4, 5, 6] # extract until the last index
>>> print(list[:3])
[1, 2, 3] # extract from the initial (0th ) index
>>>

以相反的顺序打印列表的值 (Printing the values of list in a reverse order)

Using the negative step value, we could print the items in the list in a reverse order.

使用负步长值,我们可以以相反的顺序打印列表中的项目。

Example:

例:

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> list = [1,2,3,4,5,6]
>>> print(list[-1:1:-1])
[6, 5, 4, 3]
>>> print(list[::-1])
[6, 5, 4, 3, 2, 1] # reversed order of all elements in list
>>>

切片字符串值 (Slicing in string values)

Slicing can also be applied on to a string variables. Consider the following examples,

切片也可以应用于字符串变量。 考虑以下示例,

Example 1: Reverse the string

示例1:反转字符串

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> test_str = "http://www.includehelp.com"
>>> print(test_str[::-1])
moc.plehedulcni.www//:ptth
>>>

Example 2: Get the top level domain

示例2:获取顶级域

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> test_str = "http://www.includehelp.com"
>>> print(test_str[-4:])
.com
>>>

Example 3: Print the URL with the protocol

示例3:使用协议打印URL

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> test_str = "http://www.includehelp.com"
>>> print(test_str[7:])
www.includehelp.com

Example 4: Print the URL without the protocol or top level domain

示例4:打印没有协议或顶级域的URL

Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> test_str = "http://www.includehelp.com"
>>> print(test_str[7:-4])
www.includehelp
>>>


翻译自: https://www.includehelp.com/python/slice-notation.aspx

python中切片

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值