[PY3]——内置数据结构(9)——线性结构与切片/命名切片slice()
线性结构的总结
列表list 元组tuple 字符串str bytes bytearray的共同点:
- 都是顺序存储、顺序访问的;
- 都是可迭代对象;
- 都可以通过索引访问
线性结构的特征:
- 可迭代
- 可通过len获取长度
- 可以使用下标操作符通过索引访问
- 可以切片
补充三个函数:
- enumerate() 同时获取index和value
- next()获取一个迭代器的下一个值
- iter()把一个可迭代对象转化成一个迭代器
切片操作[start:stop]
# 注意:以下几条规则的优先级,是按照顺序依次排序的 In [12]: lst=list(range(10))
In [13]: lst
Out[13]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # 访问lst的一段,从start开始到stop结束,但不包含stop
In [14]: lst[3:7]
Out[14]: [3, 4, 5, 6] # 当start省略时表示从头开始,当stop省略时表示直到末尾
In [15]: lst[:7]
Out[15]: [0, 1, 2, 3, 4, 5, 6] In [16]: lst[3:]
Out[16]: [3, 4, 5, 6, 7, 8, 9] In [17]: lst[:]
Out[17]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # 支持负数索引,负数索引实际上可以转化为 len(lst)+index
In [18]: lst[-5:-3] //[10+(-5)=5,10+(-3)=7]
Out[18]: [5, 6] In [25]: lst[3:-1] //[3,10+(-1)=9]
Out[25]: [3, 4, 5, 6, 7, 8] # 当start>=stop时,返回空列表
In [21]: lst[100:] //100>-1
Out[21]: [] In [22]: lst[:-100] //0>10+(-100)
Out[22]: [] In [23]: lst[3:1] //3>1
Out[23]: [] In [24]: lst[3:3] //3=3
Out[24]: [] # 当start超出索引范围start=0,当stop超出索引范围stop=-0
In [19]: lst[-100:]
Out[19]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [20]: lst[-100:100]
Out[20]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[start:stop:step]
In [27]: lst[::2]
Out[27]: [0, 2, 4, 6, 8] In [28]: lst[::-2]
Out[28]: [9, 7, 5, 3, 1] In [30]: lst[::-1]
Out[30]: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] In [35]: lst[10:1:-2]
Out[35]: [9, 7, 5, 3] In [36]: lst[10:1:2]
Out[36]: []
命名切片slice( )
items=[0,1,2,3,4,5,6]
a=slice(2,4)
print(a.start,a.stop,a.step) #切片的start、stop、step三元组slice()中都可以指定
2 4 None
print(a)
slice(2, 4, None) #使用已命名的a,可以做什么?
print(items[a]) #等价于print(items[2:4])
[2, 3]
items[a]=[10,11];print(items)
[0, 1, 10, 11, 4, 5, 6]
del items[a];print(items)
[0, 1, 4, 5, 6]
[PY3]——内置数据结构(9)——线性结构与切片/命名切片slice()的更多相关文章
- [PY3]——内置数据结构(6)——集合及其常用操作
集合及其常用操作Xmind图 集合的定义 # set( ) # {0,1,2} //注意不能用空的大括号来定义集合 # set(可迭代对象) In [1]: s=set();type ...
- [PY3]——内置数据结构(8)——解构与封装
### 解构的理解与用法 ### 解构是python很有特色的一个功能,被很多语言借鉴(例如ES6) # 元素按照顺序赋值给变量 In [31]: lst=list(range(5)) In [32] ...
- [PY3]——内置数据结构(7)——字典及其常用操作
字典及其常用操作Xmind图 关于字典 字典是一种key-value结构 字典是无序的 字典的定义 # {}大括号可以直接定义一个空字典 In [1]: d={};type(d) Out[1]: di ...
- [PY3]——内置数据结构(1)——列表及其常用操作
列表及其常用操作_xmind图 about列表 列表是一个序列,用于顺序存储数据 列表分为两种:ArrayList(用数组实现).LinkedList(用链表实现) 定义与初始化 #l ...
- [PY3]——内置数据结构(5)——字符串编码
py2和py3中关于字符串的最大区别? python2中只有 unicode类型 而python3中有 string bytes两种类型 关于string和bytes的区分? 1.str是文本序列.b ...
- [PY3]——内置数据结构(2)——元组及其常用操作
定义和初始化 #tuple() 使用工厂函数tuple定义一个空元组 #() 使用圆括号定义一个空元组 #(1,2,3) 使用圆括号定义有初始值的元组 #tuple(可迭代对象) 把可迭代对象转换为一 ...
- [PY3]——内置数据结构(3)——字符串及其常用操作
字符串及其常用操作xmind图 字符串的定义 1. 单引号/双引号 In [1]: s1='hello world' In [2]: s1="hello world" 2. 三对单 ...
- [PY3]——内置数据结构(4)——字符串格式化(format)
字符串格式化是拼接字符串的一种手段 join和+拼接字符串的方法,难以控制格式 printf style 字符串格式化 这种方法是从c语言继承过来的 # 待格式化的字符串:一个字符串存在占位符 In ...
- 内置数据结构(list)
列表.元组.字符串.字典和集合是python内置的数据结构,也可以叫内置容器.前3个是线性结构,线性结构可以切片操作.解包和封包操作. dir()方法可以查看对象拥有哪些属性和方法. help()方法 ...
随机推荐
- Buffer Pool--内存总结1
物理地址空间是处理器用来访问位于总线上的所有部件的集合.在32位处理器上,地址总线为32位,寻址空间为4GB.在使用PAE的32位服务器上,地址总线为36位,寻址空间为64GB.在64位的处理器上生产 ...
- ASP.NET Core IdentityServer4 新手上路
OAuth2.0资料 今天看到一篇博主写了该系列文章,贴图和过程都比较详细,俗话说实践是检验真理的唯一标准(如果是按照参考文章复制粘贴,应该不会出现踩坑,但是我喜欢自己手动敲一遍),发现几个坑,因而总 ...
- Ocelot 新手上路
新手上路,老司机请多多包含!Ocelot 在博园里文章特别多,但是按照其中一篇文章教程,如果经验很少或者小白,是没法将程序跑向博主的结果. 因此总结下 参考多篇文章,终于达到预期效果. Oce ...
- Polynomial ( Arithmetic and Algebra) CGAL 4.13 -User Manual
1 Fundamentals A polynomial is either zero, or can be written as the sum of one or more non-zero ter ...
- Codeforces Beta Round #75 (Div. 1 Only) B. Queue 二分
B. Queue Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/91/B Descrip ...
- D - How Many Tables (并查集)(水题)
点击打开链接 Today is Ignatius' birthday. He invites a lot of friends. Now it's dinner time. Ignatius want ...
- PHP中运算符优先级
运算符优先级指定了两个表达式绑定得有多“紧密”.例如,表达式 1 + 5 * 3 的结果是 16 而不是 18 是因为乘号(“*”)的优先级比加号(“+”)高.必要时可以用括号来强制改变优先级.例如: ...
- 如何实现三个div都自适应(滴滴面试题)
<div class="table"> <div class="accordant"> <div class="box& ...
- mangodb的存储
mongodb基本命令 mongo #进入mongo命令行show dbs #查看所有数据库 use tutorial #切换到名为tutorial的数据库 show collections #查看数 ...
- pandas.concat连接dataframe
https://blog.csdn.net/stevenkwong/article/details/52528616