python中index、slice与slice assignment用法
python中index、slice与slice assignment用法
一、index与slice的定义:
- index用于枚举list中的元素(Indexes enumerate the elements);
- slice用于枚举list中元素集合(Slices enumerate the spaces between the elements).
- slice assignment,是一个语法糖,主要用于list的快速插入、替换、删除元素。
二、语法
index
index语法很简单,如下表所示,如有一个list a,a[i]表示a中第i个元素(a[0]表示第一个元素),当i<0时,表示倒数第几个元素(a[-1]表示倒数第一个元素)。
Python indexes and slices for a six-element list.
Indexes enumerate the elements, slices enumerate the spaces between the elements.
Index from rear: -6 -5 -4 -3 -2 -1 a=[0,1,2,3,4,5] a[1:]==[1,2,3,4,5]
Index from front: 0 1 2 3 4 5 len(a)==6 a[:5]==[0,1,2,3,4]
+---+---+---+---+---+---+ a[0]==0 a[:-2]==[0,1,2,3]
| a | b | c | d | e | f | a[5]==5 a[1:2]==[1]
+---+---+---+---+---+---+ a[-1]==5 a[1:-1]==[1,2,3,4]
Slice from front: : 1 2 3 4 5 : a[-2]==4
Slice from rear: : -5 -4 -3 -2 -1 :
b=a[:]
b==[0,1,2,3,4,5] (shallow copy of a)
slice
slice的基本形式为a[start : end : step],这三个参数都有默认的缺省值,但是为了与index区别,slice语法中至少要有一个:,对缺省值的理解是正确使用slice的关键,可以用一下几条来说明:
step的默认值为1,若step > 0表示从前向后枚举,step < 0则相反,step不能为0;
start、end的默认值与step值的正负相关,可见下表:
-------------------------
step | + | - |
-------------------------
start | 0 | -1 |
end | len | -(len+1)|
-------------------------
step和end中的负值都可以通过加上len得到其正值,有别于range(start,end)。
slice assignment
a[start : end : step] = b与b = a[start : end : step]不同,后者是在list a中取出一些元素,然后重新组成一个新的list给b,不会改变list a的值;而前者直接改变a的值。其主要用法有:
- 插入
>>> a = [1, 2, 3]
>>> a[0:0] = [-3, -2, -1, 0]
>>> a
[-3, -2, -1, 0, 1, 2, 3]
- 删除
>>> a
[-3, -2, -1, 0, 1, 2, 3]
>>> a[2:4] = []
>>> a
[-3, -2, 1, 2, 3]
- 替换
>>> a
[-3, -2, 1, 2, 3]
>>> a[:] = [1, 2, 3]
>>> a
[1, 2, 3]
引用:
--http://stackoverflow.com/questions/509211/explain-pythons-slice-notation
python中index、slice与slice assignment用法的更多相关文章
- python中的 try...except...finally 的用法
python中的 try...except...finally 的用法 author:headsen chen date:2018-04-09 16:22:11 try, except, final ...
- Python中生成器和yield语句的用法详解
Python中生成器和yield语句的用法详解 在开始课程之前,我要求学生们填写一份调查表,这个调查表反映了它们对Python中一些概念的理解情况.一些话题("if/else控制流" ...
- python中列表元素连接方法join用法实例
python中列表元素连接方法join用法实例 这篇文章主要介绍了python中列表元素连接方法join用法,实例分析了Python中join方法的使用技巧,非常具有实用价值,分享给大家供大家参考. ...
- Python中yield和yield from的用法
yield python中yield的用法很像return,都是提供一个返回值,但是yield和return的最大区别在于,return一旦返回,则代码段执行结束,但是yield在返回值以后,会交出C ...
- python中List的sort方法的用法
python列表排序 简单记一下python中List的sort方法(或者sorted内建函数)的用法. 关键字: python列表排序 python字典排序 sorted List的元素可以是各种东 ...
- python 中 dict 的另一种用法
python 中 dic另一种用法 描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 dict={} dict.get(k1,Non ...
- 【转】Python中string的strip,lstrip,rstrip用法
Python中的strip用于去除字符串的首尾字符串,同理,lstrip用于去除左边的字符,rstrip用于去除右边的字符. 这三个函数都可传入一个参数,指定要去除的首尾字符. 需要注意的是,传入的是 ...
- python 中 sorted() 和 list.sort() 的用法
今天用python自带的sorted对一个列表进行排序, 在这里总结一下 只要是可迭代对象都可以用sorted . sorted(itrearble, cmp=None, key=None, reve ...
- python中面向对象元类的自定义用法
面向对象中的常用方法 1.instance 和 issubclass instance :判断两个对象是不是一类 issubclass :判断某个类是不是另一个类的子类 #两个常用方法的使用 clas ...
随机推荐
- Android GreenDao 使用教程
上一篇 总结了grendao 环境搭建以及简单的增删查改,接下来将全面解析框架的使用,基于上篇的orm模型(Note)数据库讲解 GreenDao的插入: 插入的方式有很多: daoSession.g ...
- SQL where 条件顺序对性能的影响有哪些
经常有人问到oracle中的Where子句的条件书写顺序是否对SQL性能有影响,我的直觉是没有影响,因为如果这个顺序有影响,Oracle应该早就能够做到自动优化,但一直没有关于这方面的确凿证据.在网上 ...
- Side effect (computer science)
In computer science, a function or expression is said to have a side effect if it modifies some stat ...
- Hihocoder1350-Binary Watch
时间限制:10000ms单点时限:1000ms内存限制:256MB 描述 Consider a binary watch with 5 binary digits to display hours ( ...
- CGI与ISAPI的区别(转)
一 CGI原理及其性能 1) CGI概念CGI即通用网关接口(Common Gateway Interface),它是一段程序,运行在服务器上,提供同客户端HTML页面的交互,通俗的讲CGI就象是一座 ...
- elastic学习笔记
要点 不同工具之间版本匹配很重要由点及面,先实践起来再学细节的原理和使用 技术栈 laravel5.5框架+scout组件+elasticsearch6.3.0搜索引擎 辅助 elasticsearc ...
- Python学习笔记(4)列表
2019-02-26 列表(list):①创建方法:用‘[ ]’,将数据包括起来,数据之间用逗号隔开.②空列表:empty = []③增删改查: 1)增加: a.append()方法——将元素添加到列 ...
- 前后端交互&交互接口
前后端数据交互之数据接口 废话就不多说了,我们都知道,前端通常会通过后台提供的接口来获取数据来完成前端页面的渲染. 1.前端通过接口调用后台返回的数据 <!DOCTYPE html PUBLIC ...
- 【【henuacm2016级暑期训练】动态规划专题 D】Writing Code
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 二维费用背包. f[i][j][k] 前i个人,写了j行,bug不超过k的方案数. 可以把每个人看成是一个物品. 它可以无限拿.然后 ...
- 洛谷 P3420 [POI2005]SKA-Piggy Banks
P3420 [POI2005]SKA-Piggy Banks 题目描述 Byteazar the Dragon has NN piggy banks. Each piggy bank can eith ...