day-07
1. 元组内置方法
-
什么是元组:只可取,不可更改的列表
-
元组的作用:元组一创建就被写死了
-
定义方式:()内用逗号隔开多个任意数据类型元素
tup = () # 定义一个空元组 tup2 = (1,) # 当只有一个元素时,必须加逗号 tup3 = (1, 2, 3) tup4 = tuple(元素) # 使用tuple()定义
-
使用方法:
-
索引取值:
[索引位置]
tup=(1,2,3,4,5) print(tup[0])
运行结果:
1 Process finished with exit code 0
-
索引切片:
[起始位置:结束位置]
tup=(1,2,3,4,5) print(tup[0:3])
运行效果:
(1, 2, 3) Process finished with exit code 0
-
遍历:
for循环
tup=(1,2,3,4,5) for i in tup: print(i)
运行效果:
1 2 3 4 5 Process finished with exit code 0
-
成员运算:
in
ornot in
tup=(1,2,3,4,5) print(0 in tup)
运行效果:
False Process finished with exit code 0
-
长度:
len()
tup=(1,2,3,4,5) print(len(tup))
运行效果:
5 Process finished with exit code 0
-
获取元素索引:
index(元素)
tup=(1,2,3,4,5) print(tup.index(1))
运行效果:
0 Process finished with exit code 0
-
计数:
count(元素)
tup=(1,2,3,4,5) print(tup.count(2))
运行效果:
1 Process finished with exit code 0
-
-
有序 or 无序
有序。
-
可变 or 不可变
元组本身就不可改变,没有这个说法。
2. 字典内置方法
-
作用:存储多个具有描述意义的数据
-
定义方式:{}内用逗号隔开多个键key(具有描述意义,不能为可变数据类型):值value(任意数据类型)对
dic = {'name': 'tom'}
-
使用方法:
-
优先掌握:
-
按key取值/修改值:
[key]
/[key] = 新值
dic = {'a': 1, 'b': 2, 'c': 3} print(dic['a']) # 取出key为 a 的数据的值 dic['b'] = 4 # 修改key为 b 的数据的值为 4 print(dic)
运行结果:
1 {'a': 1, 'b': 4, 'c': 3} Process finished with exit code 0
-
添加值:
[新key] = 新值
dic = {'a': 1, 'b': 2, 'c': 3} dic['d'] = 4 # 添加新的键值对,如果key在字典已经存在,则修改其值为新值 print(dic)
运行结果:
{'a': 1, 'b': 2, 'c': 3, 'd': 4} Process finished with exit code 0
-
遍历:
for 循环
(只能取出字典的key)dic = {'a': 1, 'b': 2, 'c': 3} for i in dic: print(i)
运行结果:
a b c d Process finished with exit code 0
-
成员运算:
in
ornot in
dic = {'a': 1, 'b': 2, 'c': 3} print('a' in dic)
运行结果:
True Process finished with exit code 0
-
长度:
len()
dic = {'a': 1, 'b': 2, 'c': 3} print(len(dic))
运行结果:
4 Process finished with exit code 0
-
获取所有的键/值/键值对:
keys
/values
/items
dic = {'a': 1, 'b': 2, 'c': 3} print(dic.keys()) # 获取所有键 print(dic.values()) # 获取所有值 print(dic.items()) # 获取所有键值对
运行结果:
dict_keys(['a', 'b', 'c']) dict_values([1, 2, 3]) dict_items([('a', 1), ('b', 2), ('c', 3)]) Process finished with exit code 0
运行结果:
可以使用 for 循环遍历得到所有的键值对
dic = {'a': 1, 'b': 2, 'c': 3} for i in dic.items(): print(i)
运行结果:
('a', 1) ('b', 2) ('c', 3) Process finished with exit code 0
进阶写法:
dic = {'a': 1, 'b': 2, 'c': 3} for k, v in dic.items(): # 解压缩 print(k, v)
运行结果:
a 1 b 2 c 3 Process finished with exit code 0
-
-
需要掌握
-
获取:
.get(key)
dic = {'a': 1, 'b': 2, 'c': 3} print(dic.get('b')) # 获取 key 为 b 的值 print(dic.get('d', 0)) # 获取 key 为 d 的值,如果没有,则返回0,可以自定义,不设置则返回None print(dic.get('d'))
运行结果:
2 0 None Process finished with exit code 0
-
更新:
.update()
dic1 = {'a': 1, 'c': 2} dic2 = {'b': 1, 'd': 2} dic1.update(dic2) print(dic1)
运行结果:
{'a': 1, 'c': 2, 'b': 1, 'd': 2} Process finished with exit code 0
-
.fromkeys(seq, value)
:以列表 seq 为 key ,值为 value ,创建一个新字典# seq = ('Google', 'Runoob', 'Taobao') 字典键值列表 # value -- 可选参数, 设置键序列(seq)的值,默新字典的值为 None seq = ('Google', 'Runoob', 'Taobao') dic = dict.fromkeys(seq) print(dic) dict = dict.fromkeys(seq, 10) print(dic)
运行结果:
{'Google': None, 'Runoob': None, 'Taobao': None} {'Google': 10, 'Runoob': 10, 'Taobao': 10} Process finished with exit code 0
-
setdefault
:如果键不存在于字典中,将会添加键并将值设为默认值dic = {'a': 1, 'b': 2, 'c': 3} dic.setdefault('j', 2) dic.setdefault('a', 2) print(dic)
运行结果:
{'a': 1, 'b': 2, 'c': 3, 'j': 2} Process finished with exit code 0
-
-
-
有序 or 无序
无序
-
可变 or 不可变
可变
3. 集合内置方法
-
什么是集合:无序的不重复元素序列
-
集合的作用:
- 运算:交集/并集/补集/差集
- 去重:同样的值存的位置是一样的,拿到第一个就不会拿到第二个
- 乱序:插值是按照某种哈希算法随机插入的
-
定义方式:
-
定义一个空集合,必须使用
set()
**s = set() # 空集合
-
{}内以逗号隔开多个元素(不能可为可变数据类型)
s = {'a', 'a', 1, 'b', 2, 2, 'c', 3, 3, 4, 5, 6} # 对于数字而言,不会乱序;但是对于其他,就乱序 print(s)
运行结果:
{1, 2, 3, 4, 5, 6, 'a', 'b', 'c'} Process finished with exit code 0
-
-
使用方法
-
运算
a = set('abracadabra') b = set('alacazam') print(a & b ) # 集合a和b中都包含了的元素 print(a - b ) # 集合a中包含而集合b中不包含的元素 print(a | b ) # 集合a或b中包含的所有元素 print(a ^ b) # 不同时包含于a和b的元素
运行结果:
{'c', 'a'} {'d', 'r', 'b'} {'c', 'b', 'z', 'd', 'a', 'r', 'm', 'l'} {'r', 'm', 'b', 'l', 'z', 'd'} Process finished with exit code 0
-
添加元素:
.add(元素)
:如果元素不存在,则添加,如果存在,则不进行任何操作s = set() s.add(1) print(s)
运行结果:
{1} Process finished with exit code 0
-
随机删除:
.pop()
:随机删除集合内的一个元素s = {'a', 'r', 'b', 'c', 'd'} s.pop() print(s)
运行结果:
{'r', 'a', 'b', 'd'} # 第一次运行 {'d', 'b', 'a', 'r'} # 第二次运行 {'d', 'r', 'a', 'c'} #第三次运行 Process finished with exit code 0
-
-
有序 or 无序
无序
-
可变 or 不可变
可变
4. 数据类型总结
- 按照 存值个数 分类
- 存一个值
- 整形
- 浮点型
- 字符串
- 存多个值
- 列表
- 元组
- 字典
- 集合
- 存一个值
- 按照 有序 or 无序 分类
- 有序(序列类型)
- 字符串
- 列表
- 元组
- 无序
- 字典
- 集合
- 有序(序列类型)
- 按照 可变 or 不可变 分类
- 可变
- 列表
- 字典
- 集合
- 不可变
- 整型
- 浮点型
- 字符串
- (元组)
- 可变
5. 深浅拷贝
假设有一个原始列表:lt1 = [1, 2, 3, [4, 5, 6]]
,其中同时包含可变数据类型和不可变数据类型,我们对其分别进行拷贝、浅拷贝、深拷贝得到一个新列表,然后改变原始列表的值,观察新列表。
5.1 拷贝(赋值)
用列表 lt2
去拷贝列表 lt1
lt1 = [1, 2, 3, [4, 5, 6]]
lt2 = lt1
打印两个列表元素的内存地址如下:
id(lt1) 1789594349192
id(lt1[0]): 140711988719872
id(lt1[1]): 140711988719904
id(lt1[2]): 140711988719936
id(lt1[3]): 1789594349128
id(lt1[3][0]): 140711988719968
id(lt1[3][1]): 140711988720000
id(lt1[3][2]): 140711988720032
******************************
id(lt2) 1789594349192
id(lt2[0]) 140711988719872
id(lt2[1]) 140711988719904
id(lt2[2]) 140711988719936
id(lt2[3]) 1789594349128
id(lt2[3][0]) 140711988719968
id(lt2[3][1]) 140711988720000
id(lt2[3][2]) 140711988720032
Process finished with exit code 0
- 当
lt1
内部的不可变元素发生改变时,观察lt2
lt1 = [1, 2, 3, [4, 5, 6]]
lt2 = lt1
lt1.append(7)
print(lt1)
print(lt2)
打印两个列表如下:
[1, 2, 3, [4, 5, 6], 7]
[1, 2, 3, [4, 5, 6], 7]
Process finished with exit code 0
- 当
lt1
内部的可变元素发生改变时,观察lt2
lt1 = [1, 2, 3, [4, 5, 6]]
lt2 = lt1
lt1[3].append(7)
print(lt1)
print(lt2)
打印两个列表如下:
[1, 2, 3, [4, 5, 6, 7]]
[1, 2, 3, [4, 5, 6, 7]]
Process finished with exit code 0
观察可以知道,当 lt2
为 lt1
的拷贝对象时, lt1
内部的不可变数据发生变化, lt2
也会随之变化; lt1
内部的可变数据变化, lt2
也会随之变化。
总结1:当 lt2
为 lt1
的拷贝对象时,只要 lt1
发生了变化, lt2
都会随之改变。
5.2 浅拷贝
用列表 lt2
去浅拷贝列表 lt1
import copy
lt1 = [1, 2, 3, [4, 5, 6]]
lt2 = copy.copy(lt1) # lt2叫做lt1的浅拷贝对象
打印两个列表元素的内存地址如下:
id(lt1) 1811455389320
id(lt1[0]): 140712045080832
id(lt1[1]): 140712045080864
id(lt1[2]): 140712045080896
id(lt1[3]): 1811455389256
id(lt1[3][0]): 140712045080928
id(lt1[3][1]): 140712045080960
id(lt1[3][2]): 140712045080992
******************************
id(lt2) 1811455389576
id(lt2[0]) 140712045080832
id(lt2[1]) 140712045080864
id(lt2[2]) 140712045080896
id(lt2[3]) 1811455389256
id(lt2[3][0]) 140712045080928
id(lt2[3][1]) 140712045080960
id(lt2[3][2]) 140712045080992
Process finished with exit code 0
- 当
lt1
内部的不可变元素发生改变时,观察lt2
lt1 = [1, 2, 3, [4, 5, 6]]
lt2 = lt1
lt1.append(7)
print(lt1)
print(lt2)
打印两个列表如下:
[1, 2, 3, [4, 5, 6], 7]
[1, 2, 3, [4, 5, 6]]
- 当
lt1
内部的可变元素发生改变时,观察lt2
lt1 = [1, 2, 3, [4, 5, 6]]
lt2 = lt1
lt1[3].append(7)
print(lt1)
print(lt2)
再次打印两个列表如下:
[1, 2, 3, [4, 5, 6, 7]]
[1, 2, 3, [4, 5, 6, 7]]
观察可以知道,当 lt2
为 lt1
的浅拷贝对象时, lt1
内部的不可变数据发生变化时, lt2
不会随之变化; lt1
内部的可变类型发生变化时, lt2
会随之变化。
总结2:当 lt2
为 lt1
的浅拷贝对象时,当 lt1
内部的不可变数据发生变化时, lt2
不会随之改变,当 lt1
内部的可变数据发生变化时, lt2
会随之改变。
5.3 深拷贝
用列表 lt2
去深拷贝列表 lt1
import copy
lt1 = [1, 2, 3, [4, 5, 6]]
lt2 = copy.copy(lt1) # lt2叫做lt1的浅拷贝对象
打印两个列表如下:
id(lt1) 2541724348168
id(lt1[0]): 140712045080832
id(lt1[1]): 140712045080864
id(lt1[2]): 140712045080896
id(lt1[3]): 2541724348104
id(lt1[3][0]): 140712045080928
id(lt1[3][1]): 140712045080960
id(lt1[3][2]): 140712045080992
******************************
id(lt2) 2541724348360
id(lt2[0]) 140712045080832
id(lt2[1]) 140712045080864
id(lt2[2]) 140712045080896
id(lt2[3]) 2541724819784
id(lt2[3][0]) 140712045080928
id(lt2[3][1]) 140712045080960
id(lt2[3][2]) 140712045080992
- 当
lt1
内部的不可变元素发生改变时,观察lt2
lt1 = [1, 2, 3, [4, 5, 6]]
lt2 = lt1
lt1.append(7)
print(lt1)
print(lt2)
打印两个列表如下:
[1, 2, 3, [4, 5, 6], 7]
[1, 2, 3, [4, 5, 6]]
- 当
lt1
内部的可变元素发生改变时,观察lt2
lt1 = [1, 2, 3, [4, 5, 6]]
lt2 = lt1
lt1[3].append(7)
print(lt1)
print(lt2)
再次打印两个列表如下:
[1, 2, 3, [4, 5, 6, 7]]
[1, 2, 3, [4, 5, 6]]
观察可以知道,当 lt2
为 lt1
的深拷贝对象时, lt1
内部的不可变数据发生变化时, lt2
不会随之变化; lt1
内部的可变数据发生变化时, lt2
也不会随之变化。
总结3:当 lt2
为 lt1
的深拷贝对象时,不论 lt1
内部的数据有没有变化, lt2
都不会改变。