这是 python 入门教程的第三篇的内容, 会具体介绍在 Python 中常用的四种数据结构, 分别是列表, 元祖, 集合, 字典, 这些内容在之后是很常用的, 所以要对每一种数据结构都要有所理解
关于行间距的问题, 我今天重新看了一下, 好像代码那里行间距已经是最小了, 没有办法再调整了, 用手机看的话会有些大, 如果要更好的阅读效果的话, 可以点击阅读原文, 在网站上浏览, 效果会好一些
python 列表, 元祖, 集合, 字典
这一篇我们会讲 python 中常用的四种数据结构, 列表, 元祖, 集合, 字典
- . List []
- . Tuple ()
- . set {}
- . dict {key:value}
列表 List
定义
animals = [tiger,bear,lion]
末尾增加 - append
调用 append 方法向列表末尾添加元素
- animals.append(horse)
- animals
- [tiger,bear,lion,horse]
索引
我们可以通过索引来访问列表中的每个元素, 第一个元素的索引是0, 最后一个元素的索引是 - 1
- animals[0]
- tiger
- animals[-1]
- horse
我们可以使用 len 获取列表长度
len(animals)
4
任意位置添加 insert
上面我们讲了 append 是向列表的末尾进行添加, 下面我们讲得 insert 可以向列表任意位置进行添加
- animals.insert(1,eagle)
- animals
- [tiger, eagle, bear, lion, horse]
删除任意指定值
我们使用 remove 删除指定的值
- animals.remove(tiger)
- animals
- [eagle, bear, lion, horse]
注意: 如果 tiger 出现多次, 只有第一个 tiger 会被删除
按索引删除关键字
- del animals[1]
- animals
- [eagle, lion, horse]
列表反转
我们要记住列表是有序的, 这个可以与其他数据结构进行比较, 所以我们可以对有序的列表进行反转
- animals.reverse()
- animals
- [horse, lion, eagle]
列表的合并
- animals.extend([tiger,bear])
- animals
- [horse, lion, eagle, tiger, bear]
查找
- animals.index(lion)# 返回索引
- 1
- animals.count(lion)# 返回出现次数
- 1
- lion inanimls# 返回布尔类型
- True
修改
- animals[0] = horses
- animals
- [horses, lion, eagle, tiger, bear]
pop 的使用
pop 返回列表的最后一个元素, 在返回这个元素的同时也会删除这个元素, 若传入一个参数 pop(i)则将第 i 个元素弹出
- for i inrange(len(animals)):
- animal = animals.pop()
- print(animal) #把整个列表倒叙输出>
- bear
- tiger
- eagle
- lion
- horses
- animals #此时列表是空的
- []
- animals.append(tiger)# 我们可以往里面添加值
- animals
- [tiger]
List 乘法
- list1 = [1,2,3]
- list1*3
- [1, 2, 3, 1, 2, 3, 1, 2, 3]# 列表重复三次
元祖 Tuple
元祖是一种特殊的列表, 不同点是元祖一旦创建就不能修改, 上述修改列表内容的操作例如 sort(),append()等对元祖都不在适用
在编写程序的时候, 元祖比列表更安全, 如果是只读数据, 尽可能使用元祖
创建元祖
animals = (tiger,lion,bear)
注意
如果创建的元祖只有一个元素, 需要在元素后面跟一个逗号
- animals =(tiger)
- type(animals)# 可以看到这里是 str 类型
- str
- animals
- tiger
- animals =(tiger,)
- type(animals)
- tuple
- animals
- (tiger,)
集合 Set
集合是一个无序不重复的数据集, 对比列表, 首先是无序的, 不可以使用索引来进行访问, 另一个特点是不能有重复的数据
集合支持数学上的一些运算, 例如: union(并集),intersection(交集),difference(补集)
集合的创建
集合的创建使用 set 函数或者 {}, 注意空集合的创建只能使用 set 函数, 使用{} 创建的是一个空的字典
- animals ={tiger,bear,lion,eagle,tiger}
- animals
- {bear,eagle, lion, tiger} #可以看到重复的元素已经被去除
- animals =set([tiger,bear,lion,eagle,tiger])
- type(animals)
- set
- animals
- {bear, eagle, lion, tiger} #可以看到重复的元素已经被去除
查看元素是否在集合中
- tiger in animals
- True
- tigersin animals
- False
- tigersnot in animals #注意这里是 not in
- True
集合的增删
- animals.add(horse)
- animals
- {bear, eagle, horse, lion, tiger}
- animals.remove(horse)
- animals
- {bear, eagle, lion, tiger}
集合子集的概念
<= 属于
- set1 ={1,2,3}
- set2 ={2,3}
- set2 inset1
- False #思考这是为什么
- set2 <=set1
- True
集合的运算
- set1 = {1,2,3,4}
- set2 ={3,4,5,6}
. | 操作, 存在 set1 中或 set2 中的元素, 等效于 union 操作
- set1|set2
- {1,2,3,4,5,6}
- set1.union(set2)
- {1,2,3,4,5,6}
. & 操作, 返回即在 set1 又在 set2 的元素
set1&set2
{3,4}
. - 返回在 set1 不在 set2 的元素
set1-set2
{1,2}
. ^ 操作, 返回只存在两个集合中的元素
- set1^set2
- {1,2,5,6}
字典 dict
字典是无序的键值对集合字典中的每一个元素都是一个 key 和一个 value 的组合, key 值在字典中必须是唯一的, 因此很方便的从字典中使用 key 来获取其对应的 value 的值
字典的创建
- animals = {1:tiger,2:lion,3:horse}
- animals[1]
- tiger
字典中 value 的获取
如果 key 不存在, 那么 dict[key]会抛出错误, 有时候为了避免错误的出现, 我们会使用 get()函数来获取 key 对应的 value, 如果这个 key 不存在, 那么就默认返回 None
- animals.get(1)
- tiger
- animals.get(4,default)
- default #key 不存在的时候, 返回默认值
增加元素
- animals[4]=eagle #只需要为字典中 key 进行赋值
- animals
- {1: tiger, 2: lion, 3: horse, 4: eagle}
删除元素
- del animals[4]
- animals
- {1: tiger, 2: lion, 3: horse}
获取 keys 和 values
- list(animals.keys())
- [1,2,3]
- list(animals.values())
- [tiger, lion, horse]
- for valuein animals.values():
- print(value)>
- tiger
- lion
- horse
赋值与浅拷贝与深拷贝
- col = [red,blue,green]
- col_new =col
- col_new.append(black)
- col_new
- [red, blue, green, black]
- col
- [red, blue, green, black] #这个是赋值
- id_row =[id(ele) for ele in col]
- id_new =[id(ele) for ele in col_new]
我们可以看到上面两个是一样的
下面说一下深拷贝, 浅拷感兴趣可以看一下
- importcopy
- col =[red,blue,green]
- col_new =copy.deepcopy(col)
- col_new.append(black)
- col_new
- [red, blue, green, black]
- col
- [red, blue, green]
大家要温故知新呀, Python 学习是要自己上手实践的, 多把代码敲一下
来源: http://geek.csdn.net/news/detail/256204