数据类型三
2.1散列类型
1.数据类型:int , float , bool ==>存储一个数值
2.序列类型: str, list , tuple ==》存储多个数据
3.散列类型
无序 内部元素不重复 没有下标
2.1.1字典 dict
用来保存一些有典型的对应关系的数据类型,特点是使用键值对的方式来存储数据
key === > 键 value===> 值
键值对的语法:key:value
表现形式:
{},不过大括号里面如果存储的是键值对,那么就是字典
通过键查找值
字典名[键]
字典的key不可以是重复的,如果重复则取最后一个重复键的值
1
2
3
4
5
6
7
8
9
10call = {'初雪':'11234', 'look':'11235', '阿尔法':'11236'} user = {'姓名':'初雪', '年龄':18, '工作':'python'} print(call['初雪']) print(user['工作']) dict1 = {'a':1, 'b':2, 'a':3, 'a':4} print(dict1) 11234 python {'a': 4, 'b': 2}
可变性
1
2
3
4
5dict1 = {'a':1, 'b':2, 'a':3, 'a':4} dict1['b'] = 100 print(dict1) {'a': 4, 'b': 100}
当我们想要表示一组固定信息的时候,用字典就可以更加的直观
1
2
3
4
5
6
7字典包含多个键值对,key是字典的关键数据,程序对字典的操作都是基于key的 - 通过key访问value - 通过key添加key-value对 - 通过key删除key-value对 - 通过key修改key-value对 - 通过key判断指定key-value对是否存在
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25通过key访问value scores = {'语文':89} print(scores['语文']) 如果腰围字典添加key-value对,就只需要为不存的key赋值就可以了 scores['数学'] = 93 scores[92] = 8.4 print(scores) 如果要删除字典中的键值对,就可以使用del语句 del scores['语文'] del scores['数学'] print(scores) 对存在的键值对赋值,新的值会覆盖原来的值 cars = {'bmw':88, 'benchi':83, 'tesila':95} cars['benchi'] = 4.3 cars['tesila'] = 3.8 print(cars) 判断字典是否包含指定的key,使用in或者not in in not in # in 包含 not in 不包含 cars = {'bmw':88, 'benchi':83, 'tesila':95} print('tesila' in cars) print('baoshijie' in cars) print('BYD' not in cars)
字典的key可以是任意的不可变类型
字典允许直接对不存在的key赋值,这样就可以增加一个键值对
常用方法
1
2
3
4
5字典方法 print(dir(dict)) ['__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
2.1.1.1 clear()
用于清空点中所有的键值对
1
2
3
4
5
6
7cars = {'bmw':88, 'benchi':83, 'tesila':95} print(cars) cars.clear() print(cars) {'bmw': 88, 'benchi': 83, 'tesila': 95} {}
2.1.1.2 get()
根据key获取value的
1
2
3
4
5
6
7
8
9cars = {'bmw':88, 'benchi':83, 'tesila':95} print(cars.get('bmw')) print(cars.get('baoshijie')) print(cars['baoshijie']) 88 None print(cars['baoshijie']) # 报错 KeyError: 'baoshijie'
翻车的地方 (元组)
data = (99,88,77,[‘初雪’,18],33,22,11)
data[3] [0] = ‘look’
print(data)
元素不可变 但是元素内部有可变的类型 就可以改变 但只限于元组内部 可变元素
2.1.1.3 update()
可以使用一个字典所包含的键值对来更新已经有的字典,如果被更新的字典包含对应的键值对,那么原来的值会被覆盖,如果不包含,就会被添加进去
1
2
3
4
5cars = {'bmw':88, 'benchi':83,'tesila':95} cars.update({'bmw':4.5,'baoshijie':9.3}) print(cars) {'bmw': 4.5, 'benchi': 83, 'tesila': 95, 'baoshijie': 9.3}
2.1.1.4 items(), keys(), values()
dict_items,dict_keys,dict_values对象,python不希望用户直接操作这几个方法,但是可以通过list()函数把他们转成列表
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33items()==》取键值对 cars = {'bmw':88, 'benchi':83,'tesila':95} ims = cars.items() print(ims) print(type(ims)) print(list(ims)) print(list(ims)[1]) dict_items([('bmw', 88), ('benchi', 83), ('tesila', 95)]) <class 'dict_items'> [('bmw', 88), ('benchi', 83), ('tesila', 95)] ('benchi', 83) keys()==> 取键 cars = {'bmw':88, 'benchi':83,'tesila':95} kys = cars.keys() print(kys) print(type(kys)) print(list(kys)) print(list(kys)[1]) dict_keys(['bmw', 'benchi', 'tesila']) <class 'dict_keys'> ['bmw', 'benchi', 'tesila'] benchi cars = {'bmw':88, 'benchi':83,'tesila':95} vals = cars.values() print(vals) print(type(vals)) print(list(vals)) print(list(vals)[1]) dict_values([88, 83, 95]) <class 'dict_values'> [88, 83, 95] 83
程序调用字典的三个方法后,都需要调用给他们转成列表,这样就可以吧这三个方法的返回值转成列表
2.1.1.5 pop()
用于获取指定的key对应的value值,并且删除这个键值对
1
2
3
4
5
6cars = {'bmw':88, 'benchi':83,'tesila':95} print(cars.pop('tesila')) print(cars) 95 {'bmw': 88, 'benchi': 83}
2.1.1.6 setdefault()
能返回指定的key对应的value值,如果键值对不存在,就会先为这个键设置默认的值,再返回这个键对应的值
1
2
3
4
5
6cars = {'bmw':88, 'benchi':83,'tesila':95} print(cars.setdefault('baoshijie',92))#两个参数,第二个是设置默认值,如果不存在就会新增键值对 print(cars) 92 {'bmw': 88, 'benchi': 83, 'tesila': 95, 'baoshijie': 92}
2.1.2公共功能
长度
1
2
3
4
5info = {'age':12, 'shengming':True, 'name':'左手'} data = len(info) print(data) 3
索引(键)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15info = {'age':12, 'shengming':True, 'name':'左手'} print(info['name']) # 左手 字典 通过键 查找值 print(info['age']) #12 print(info['xxx']) # 报错 如果键不存在会报错 左手 12 报错 value = info.get('xxx') # 使用get方法获取值如果不存在就打印None print(value) None
for循环
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22info = {'age':12, 'shengming':True, 'name':'左手'} #直接遍历这个字典,可以获取所有的键 for item in info: print(item) #所有键 age shengming name #使用keys方法 for item in info.keys(): print(item) # 所有键 #使用values方法 for item in info.values(): print(item) # 所有值 #使用items方法 for key,value in info.items(): print(key,value) # 获取所有的键值
2.2 集合
元素都是唯一的,互不相同的
1
2
3
4{name1, name2, .....name n} 语法: 集合名 = {数据1,数据2,数据3,。。。。数据n}#如果有重复的数据,是不会被添加到内存空间里面
无法存储列表,字典,集合这些数据类型,否则会报错
1
2
3
4
5#报错 print({{'a':1}}) print({[1,2,3]}) print({{1,2,3}})
要注意的是:集合中的数据必须保证是唯一的,集合对于美中数据元素,只会保留一份,也就是去重
1
2
3
4s = {1,2,1,(1,2,3),'c','c'} print(s) {1, 2, (1, 2, 3), 'c'}
python中set是没有顺序的,每次输出时顺序都有可能不同
1
2
3
4
5
6
7set1 = {1,2,3,4,5,5,1,2,3,4,5,6,7,9,1} print(set1) # print(set1[1]) print(type(set1)) {1, 2, 3, 4, 5, 6, 7, 9} <class 'set'>
注意 :创建空集合的时候必须要使用set()而不是{},因为{}默认是空字典
1
2
3
4
5
6li1 = [] # 空列表 tu1 = () # 空元组 str1 = '' # 空字符串 dict1 = {} # 空字典 set1 = set() #空集合
1
2
3
4
5
6
7
8
9# 去重并转为列表输出 li = [1,2,3,4,5,6,7,89,41,1,1,1] # li1 = set(li) # li2 = list(li1) # print(li1) # print(li2) print(list(set(li))) [1, 2, 3, 4, 5, 6, 7, 41, 89]
2.2.1 集合的数学运算
运算符操作 | python运算符 | 含义 |
---|---|---|
交集 | & | 取两集合公共的元素 |
并集 | | | 取两集合全部的元素 |
差集 | - | 取一个集合中另一集合没有的元素 |
成员运算 | in 和 not in | 判断某个元素是否在或者不在集合中 |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20集合1 & 集合2——》判断交集 集合2 | 集合1——》判断并集 集合1 - 集合2——》判断差集 交集:两个集合里面共同有的数据 并集:两个集合里面的全部数据(不包括重复数据,集合本身也不能重复) 差集:集合1 - 集合2 减去共有的,剩下的就是差集 set1 = {1,2,3} set2 = {3,4,5} print(set1 & set2) print(set1 | set2) print(set1 - set2)# 差集{1, 2} 取一个集合中另一个集合没有的元素,将set1中属于set2的元素删除 print(set2 - set1)# 4,5 print(3 in set2) {3} {1, 2, 3, 4, 5} {1, 2} {4, 5} True
集合的作用:
- 存储非重复数据
- 用于将序列类型去重,逻辑判断(交集,并集,差集)
2.2.2 增
add() 参数为要添加的对象,通过多次添加数据可以发现添加后的元素位置不确定
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15语法: 集合名.add(元素) s = {'左手'} s.add('王博') print(s) s.add('高飞') print(s) s.add('陈冠希') print(s) s.add('奥利给') print(s) {'左手', '王博'} {'高飞', '左手', '王博'} {'高飞', '左手', '王博', '陈冠希'} {'陈冠希', '奥利给', '高飞', '王博', '左手'}
update() 参数为序列类型,会将每一个元素迭代添加到序列中(随机添加)
1
2
3
4
5
6
7
8s = {'左手'} s.update('123') print(s) s.update([4,5,6]) print(s) {'3', '2', '左手', '1'} {'2', 4, 5, 6, '3', '1', '左手'}
2.2.3 删
pop()随机删除一个元素
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16实际上在进行代码实验的时候并不是随机的 仅仅是在集合元素是字符串类型时,并且在cmd运行的时候才会随机删除,pycharm中默认保持删除第一个元素 s = {'左手'} s.update('123') print(s) s.update([4,5,6]) print(s) s.pop() print(s) s.pop() print(s) {'2', '1', '左手', '3'} {'1', 4, '左手', 5, 6, '2', '3'} {4, '左手', 5, 6, '2', '3'} {'左手', 5, 6, '2', '3'}
remove(参数)有参数,参数就是要删除的元素,如果元素不存在就报错
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18集合名.remove(元素) s = {'左手'} s.update('123') print(s) s.update([4,5,6]) print(s) s.remove('1') print(s) s.remove('2') print(s) s.remove('8') print(s) {'1', '左手', '2', '3'} {4, 5, 6, '3', '1', '2', '左手'} {4, 5, 6, '3', '2', '左手'} {4, 5, 6, '3', '左手'} 报错
discard()跟remove相似,但是元素不存在也不会报错
1
2
3
4
5
6
7
8
9
10
11s = {'左手'} s.update('123') print(s) s.update([4,5,6]) print(s) s.discard('aaa') print(s) {'3', '1', '左手', '2'} {4, 5, 6, '3', '左手', '1', '2'} {4, 5, 6, '3', '左手', '1', '2'}
clear() 清空集合中的元素
1
2
3
4
5
6
7
8
9
10
11s = {'左手'} s.update('123') print(s) s.update([4,5,6]) print(s) s.clear() print(s) {'1', '3', '2', '左手'} {'2', 4, 5, 6, '左手', '3', '1'} set()
del () 集合名(),删除集合
1
2
3
4
5
6
7
8
9# 整个吧盒子(变量)删除了,所以会报错,先删除了盒子,再去打印这个s就会报错显示盒子s不存在 s = {'左手'} s.update('123') print(s) s.update([4,5,6]) print(s) del s print(s)
2.2.4 改
1
2
3
4
5
6
7
8
9由于set中的数据没有索引,也没有办法去定位一个元素,所以没办法直接修改 先删除在添加 s = {'A', 'B', 'C', 'D'} # 把A改为E s.remove('A') s.add('E') print(s) {'B', 'E', 'C', 'D'}
2.2.5查
1
2
3
4
5
6
7
8
9set是一个可迭代对象,可以通过for循环进行遍历查询 s = {'A', 'B', 'C', 'D'} for i in s: print(i) A C D B
1
2
3
4
5
6
7练习题 在终端中获取颜色(RGBA),打印描述信息,否则提示颜色不存在 "R" -->"红色" "G" -->"绿色" "B" -->"蓝色" "A" -->"透明度"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58# 1.将列表数据[1,2,3,2,1,4,4,4,2,5,6,7,7,9,8,10,10]去重 li1 = [1,2,3,2,1,4,4,4,2,5,6,7,7,9,8,10,10] li2 = [] for i in li1: if i not in li2: li2.append(i) print(li2) #或者 print(list(set(li1))) # 2.有字典 dic = {"k1": "v1", "k2": "v2", "k3": "v3"},实现以下功能: # (1)、遍历字典 dic 中所有的key dic = {"k1": "v1", "k2": "v2", "k3": "v3"} for key in dic.keys(): print(key) # (2)、遍历字典 dic 中所有的value for value in dic.values(): print(value) # (3)、循环遍历字典 dic 中所有的key和value for key,value in dic.items(): print(key,value) # (4)、添加一个键值对"k4","v4",输出添加后的字典 dic dic['k4'] = 'v4' print(dic) # (5)、删除字典 dic 中的键值对"k1","v1",并输出删除后的字典 dic dic.pop('k1') print(dic) # (6)、获取字典dic中“k2”对应的值 print(dic.get('k2')) students = [ {'name': '左手', 'age': 18, 'score': 98, 'tel': '18888888888', 'gender': 'female'}, {'name': '天天', 'age': 20, 'score': 95, 'tel': '18888888889', 'gender': 'unkown'}, {'name': '二则', 'age': 18, 'score': 88, 'tel': '18888888810', 'gender': 'male'}, {'name': '涛婆', 'age': 16, 'score': 58, 'tel': '18888888811', 'gender': 'unkown'}, {'name': '广广', 'age': 19, 'score': 78, 'tel': '18888888812', 'gender': 'male'}, {'name': '杰哥', 'age': 17, 'score': 92, 'tel': '18888888813', 'gender': 'male'}, ] # (1)统计及格学生的个数 count = 0 #初始化一个值 for d in students: if d['score'] >= 60: count += 1 print(count) # (2)打印不及格学生的名字和对应的成绩 for d in students: #d是字典 if d['score'] < 60: print(d['name'],d['score']) # (3)删除性别不明的所有的学生 for d in students: if d['gender'] == 'unkown': #pop根据下标 remove根据值删除 clear 清空 del 列表名[下标] students.remove(d) # 删除性别不明 print(students)
最后
以上就是热情小猫咪最近收集整理的关于python数据类型进阶三数据类型三的全部内容,更多相关python数据类型进阶三数据类型三内容请搜索靠谱客的其他文章。
发表评论 取消回复