什么是元组:只可取,不可更改的列表算法
元组的做用:元组一建立就被写死了app
定义方式:()内用逗号隔开多个任意数据类型元素code
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])
运行效果:get
(1, 2, 3) Process finished with exit code 0
遍历: for循环
it
tup=(1,2,3,4,5) for i in tup: print(i)
运行效果:
1 2 3 4 5 Process finished with exit code 0
成员运算: in
or not 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 不可变
元组自己就不可改变,没有这个说法。
做用:存储多个具备描述意义的数据
定义方式:{}内用逗号隔开多个键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
or not 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 不可变
可变
什么是集合:无序的不重复元素序列
集合的做用:
定义方式:
定义一个空集合,必须使用 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 不可变
可变
假设有一个原始列表:lt1 = [1, 2, 3, [4, 5, 6]]
,其中同时包含可变数据类型和不可变数据类型,咱们对其分别进行拷贝、浅拷贝、深拷贝获得一个新列表,而后改变原始列表的值,观察新列表。
用列表 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
都会随之改变。
用列表 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
会随之改变。
用列表 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
都不会改变。