Python学习(四)数据结构 —— list tuple range
序列类型 list tuple range
list 和 tuple
list: 列表,由 [] 标识; 有序;可改变列表元素
tuple: 元组,由 () 标识; 有序;不可改变元组元素(和list的主要区别)
list 和 tuple 的创建:
print([]) # 空list
print(["a",1,True]) # 元素类型不限
print([x for x in range(0,6)]) # 列表推导式
print(list("a"),type(list("a"))) # 强制转化 print(()) # 空tuple
print((1)) # 不是tuple
print((1,)) # 单一元素tuple 一定要加,
print(("a",1,True)) # 元素类型不限
print(tuple("a"),type(tuple("a"))) # 强制转化
空list l = []
list 用一对方括号,用','隔开里面的元素 l = [a] l = ["a",1,True] 元素类型不限
列表推导式,如:[x for x in range(0,6)] (下方会详细介绍 range 及 列表推导式)
类型转换 list()
空tuple t = ()
tuple 若只有一个元素时,注意表示为 t = (1,) 一定要有逗号
tuple 用一对圆括号,用','隔开里面多个的元素 t = ("a",1,True) 元素类型不限
类型转换 tuple()
range
range 可方便的生成一个等差的序列,有两种表示 range(stop) 、range(start, stop[, step]) ; 通常用在 for循环语句中
range(stop) 表示 0 到 stop(不包含stop) 等差为1 的数,如 range(4) 表示 0 1 2 3
range(start, stop[, step]) 表示 从 start 到 stop(不包含stop) 等差为step的数;step缺省为1,可设置为负数
print(type(range(4))) # range本身就是一个type
for i in range(4):
print(i) # 0 1 2 3
for i in range(-1): # 从0计数,无值
print(i)
for i in range(4,7): # 4 5 6
print(i)
for i in range(2,7,2): # 2 4 6
print(i)
for i in range(5,2,-1): # 5 4 3
print(i)
序列操作
一般操作,不改变list本身
Operation | Result |
---|---|
x in s | True if an item of s is equal to x, else False |
x not in s | False if an item of s is equal to x, else True |
s + t | the concatenation of s and t |
s * n or n * s | n shallow copies of s concatenated |
s[i] | ith item of s, origin 0 |
s[i:j] | slice of s from i to j |
s[i:j:k] | slice of s from i to j with step k |
len(s) | length of s |
min(s) | smallest item of s |
max(s) | largest item of s |
s.index(x[, i[, j]]) | index of the first occurrence of x in s (at or after index i and before index j) |
s.count(x) | total number of occurrences of x in s |
s = ["a",1,True,["b"],2]
print("a" in s) # 判断元素存在于s
print("a" not in s) # 判断元素不存在于s
print("b" in s)
print(1.0 in s) # 这边不判断int float类型不同
print("" in s) # 这边的1为字符串
a = [1,2]
b = [2,1,0]
print(a+b) # 序列相加
print(a*3) # 序列乘法
s = [0,1.0,2,3,4,5,6,7,8]
print(s[0],s[2],s[3]) # 通过下标来取出对应的元素
print(type(s[0]))
print(type(s[1]))
print(s[2:4]) # 取出一段list
print(s[2:7:2]) # 根据步长取出一段list
print(len(s)) # list长度,即包含几个元素
sum = 0
for i in range(0,len(s)): # 使用for循环来取出list的每个元素
print(s[i])
sum += i # 赋值的简单表达式,相当于 sum = sum + i
print(sum) # 总和
print(min(s),max(s)) # 取最小/最大;注意元素类型间若不可比较,会报错
s = [2,3,1,2,2,3]
print(s.index(2)) # 查找对应元素第一次出现的下标
# print(s.index(4)) # 不存在该元素会报错
print(s.index(2,3)) # 从下标为3的开始找起
print(s.index(2,3,4)) # 从下标为3到下标4的阶段内找
print(s.count(2)) # 输出为2的元素的个数
print(s.count("")) # 找不到匹配元素,返回0
上方列出的操作方法对 tuple 也都适用,因为并不改变序列本身的元素,如
s = (2,3,1,2,2,3)
print(s[2],s[2:4],len(s),s.count(2)) # 对tuple均适用
改变序列的操作:仅对 list 适用;若对 tuple 操作,会报错;clear() 和 copy() 是 Python 3.3 才新增的方法
Operation | Result |
---|---|
s[i] = x | item i of s is replaced by x |
s[i:j] = t | slice of s from i to j is replaced by the contents of the iterable t |
s[i:j:k] = t | the elements of s[i:j:k] are replaced by those of t |
del s[i:j] | same as s[i:j] = [] |
del s[i:j:k] | removes the elements of s[i:j:k] from the list |
s.pop([i]) | retrieves the item at i and also removes it from s |
s.remove(x) | remove the first item from s where s[i] == x |
s.clear() | removes all items from s (same as del s[:]) |
s.append(x) | appends x to the end of the sequence (same as s[len(s):len(s)] = [x]) |
s.extend(t) | extends s with the contents of t (same as s[len(s):len(s)] = t) |
s.insert(i, x) | inserts x into s at the index given by i (same as s[i:i] = [x]) |
s.copy() | creates a shallow copy of s (same as s[:]) |
s.reverse() | reverses the items of s in place |
list的增、删、改的操作实际都比较实用,需要熟练掌握
list元素更改
可对 list 不同的下标表示法做以下操作,一般 list 下标的操作仅作对单一元素的更改赋值,如 s[0]=1 ;对多个元素的操作见下方示例(仅供参考)
s = [0,1,2,3]
s[0] = ""
print(s) # 对list的某一元素赋另外的值,类型也跟随改变
s[4] = 1 # 不可超过原list的长度,会报错
s[0:3] = [2,3,4] # 可对一段元素赋另外的值
print(s)
s[0:3] = ["x","x"] # 可缺少,元素个数也就相应的减少了
print(s)
s[0:2] = ["x","x","x","x"] # 可增加,元素个数也就相应的减加了
print(s)
s[0] = [0,0] # 单个元素注意,相当于赋值,把序列赋予该元素
print(s)
s[1:2] = [0,0]
print(s)
s = [0,1,2,3,4,5,6,7,8]
s[1:8:2] = ["x"]*4
# s[1:8:2] = ["x"]*3 # 这种表示方式元素个数一定需要相同,不然会报错
print(s)
list operation
list元素删除
s = [0,1,2,3,4,5,6,7,8]
del s[0:4] # 删除对应的元素
print(s)
s = [0,1,2,3,4,5,6,7,8]
del s[1:8:2] # 做删除
print(s)
s = [0,1,2,3,4,5,6,7,8]
s.pop(3)
print(s.pop(3),s) # 做删除,并且返回该元素的值
print(s.pop(),s) # 默认删除最后一个
s = [2,"",1.0,1,2,1]
s.remove(1) # 删除第一个值为 1 的元素
print(s)
s.clear() # 置空,Python3.3引入
print(s)
list元素增加
s = [0,1,2,3,4]
s.append(5) # list 最后加一个元素
print(s)
s.extend([6,7]) # list 最后拼接序列
print(s)
s.extend(range(3))
print(s)
s.insert(1,["x"]) # 在1的位置插入["x"]
print(s)
其他操作,reverse、copy 等
s = [1,2,3]
c = s.copy() # 相当于 c = s
print(c)
c.reverse()
print(c)
s = [2,3,1,4]
s.sort() # 排序
print(s)
# s = ["b",1,"a",True] # 报错,必须是可比较的类型
s = ["b","a"]
s.sort()
print(s)
Python学习(四)数据结构 —— list tuple range的更多相关文章
- python学习4—数据结构之列表、元组与字典
python学习4—数据结构之列表.元组与字典 列表(list)深灰魔法 1. 连续索引 li = [1,1,[1,["asdsa",4]]] li[2][1][1][0] 2. ...
- Python 学习之list和Tuple类型
1.创建list L = ['Adam', 95.5, 'Lisa', 85, 'Bart', 59] print(L) print(L[1],L[3],L[5])#索引 不能越界 正向访问 #95. ...
- Python学习记录6-list、tuple、dict、set复习
数据类型在一门语言中是非常重要的,所以选择再次学习一下加深记忆.本次主要参考了大神廖雪峰的官方网站,非常感谢大神,讲的很清晰,收获很大. 标准数据类型 Number(数字) String(字符串) L ...
- python学习笔记整理——元组tuple
Python 文档学习笔记2 数据结构--元组和序列 元组 元组在输出时总是有括号的 元组输入时可能没有括号 元组是不可变的 通过分拆(参阅本节后面的内容)或索引访问(如果是namedtuples,甚 ...
- python学习中,list/tuple/dict格式化遇到的问题
昨天上了python培训的第一课,学习了基础知识.包括类型和赋值,函数type(),dir(),id(),help()的使用,list/tuple/dict的定义以及内置函数的操作,函数的定义,控制语 ...
- Python学习笔记 - list和tuple
demo 1 #!/usr/bin/env python3 # -*- coding: utf-8 -*- >>> classmates = ['Michael', 'Bob', ' ...
- 从0开始的Python学习012数据结构&对象与类
简介 数据结构是处理数据的结构,或者说,他们是用来存储一组相关数据的. 在Python中三种内建的数据结构--列表.元组和字典.学会了使用它们会使编程变得的简单. 列表 list是处理一组有序的数据结 ...
- Python学习笔记——数据结构和算法(一)
1.解压序列赋值给多个变量 任何的序列(或者是可迭代对象)可以通过一个简单的赋值语句解压并赋值给多个变量. 唯一的前提就是变量的数量必须跟序列元素的数量是一样的. >>> data ...
- python学习之数据结构
python的数据很丰富,所以对于数据分析来讲, python是一种最合适的选择 下面讲述一下常见的数据结构,包括栈,队列,元组,字典,集合等,以及对这些数据结构进行操作 #堆栈,后进先出 a=[10 ...
随机推荐
- day2 列表中常用的方法
列表中有很多方法,下面来看看常用的方法,我们知道,字符串是以字符列表形式存储的.因此上面学习的字符串中的很多方法在列表中也有. 1.extend() extend()列表的扩展,把两个列表进行 ...
- 快速搭建sonar代码质量管理平台
安装 下载,直接解压http://www.sonarqube.org/downloads/ 添加mysql驱动至\extensions\jdbc-driver\mysql\ 创建mysql数据库和用户 ...
- 十 使用dict和set
dict Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度. 举个例子,假设要根据同学的名字 ...
- 面试的65个回答技巧-适用于BAT公司
互联网职业群分享的资料,里面大多是BAT公司的人,很多是猎头.这些技巧对于职场人来说,是非常宝贵的. 1.请你自我介绍一下你自己? 回答提示:一般人回答这个问题过于平常,只说姓名.年龄.爱好.工作经验 ...
- OOD沉思录 --- 面向动作与面向对象 --- 避免泛滥成灾的类
3.7 从设计中取出不需要的类 只有Get/Set方法的类不算是一个必要的类,Get/Set方法也不算是有意义的行为.这种类降级为属性更加合适. 3.8 去除系统外部的类 如果一个类只调用系统领域的方 ...
- Screen 常用命令+VNC 启动停止命令总结
screen -S 名称:创建一个新的会话 screen -r 会话ID:恢复一个Detach状态的会话 screen -xr 会话ID:强制恢复一个Attach状态的会话,常用于掉线时上次的会话没有 ...
- 洛谷P2730 魔板 [广搜,字符串,STL]
题目传送门 魔板 题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有 ...
- python笔记六:进程与线程
1.进程 1)调用unix/linux系统中的进程函数fork(),用法和linux相同,调用成功返回0,失败返回-1: import os print 'Process (%s) start...' ...
- linux下如何配置TCP参数设置详解
设置tcp参数一定要小心谨慎,轻易不要更改线上环境,我贴一下我们线上环境中,sysctl.conf的内容,见文章底部 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tc ...
- [APIO2018]铁人两项 --- 圆方树
[APIO2018] 铁人两项 题目大意: 给定一张图,问有多少三元组(a,b,c)(a,b,c 互不相等)满足存在一条点不重复的以a为起点,经过b,终点为c的路径 如果你不会圆方树 ------- ...