学习python的day6之字典与集合
一、字典
1.特点:
1.字典的符号是大括号
2.数据以键值对形式出现
3.各键值对用逗号分隔
创建字典
有数据的字典
dict1 = {'name':'张山','age':'19','sex':'男' }
空字典
dict2 = {}
dict3 = dict{}
注意:冒号前面的key是键,冒号后面的value是值
2.操作
增加
写法:字典序列[key] = 值
注意:如果key存在则修改这个key的值,不存在就新增这个键值对
dict1 = {'name':'张山', 'age':18, 'sex':'男'} dict1['name'] = '李四' print(dict1) dict1['id'] = 1 print(dict1) ''' 输出: {'name': '李四', 'age': 18, 'sex': '男'} {'name': '李四', 'age': 18, 'sex': '男', 'id': 1} '''
3.删除
del()/del:删除字典或删除字典中指定的键值对
dict1 = {'name':'张山', 'age':18, 'sex':'男'} del dict1['sex'] print(dict1) del dict1 print(dict1) ''' 输出: {'name': '张山', 'age': 18} Traceback (most recent call last): File "C:/Users/as_sc/PycharmProjects/untitled/hm-python-day6/01-增加数据.py", line 16, in <module> print(dict1) NameError: name 'dict1' is not defined '''
clear():清空字典
dict1 = {'name':'张山', 'age':18, 'sex':'男'} dict1.clear() print(dict1) ''' 输出: {} '''
4.修改
写法:字典序列[key] = 值
该写法的效果与新增的相同
5.查找
key值查找:如果key存在则返回对应的值,否则报错。
dict1 = {'name':'张山', 'age':18, 'sex':'男'} print(dict1['name']) print(dict1['nss']) ''' 输出: 张山 Traceback (most recent call last): File "C:/Users/as_sc/PycharmProjects/untitled/hm-python-day6/01-增加数据.py", line 35, in <module> print(dict1['nss']) KeyError: 'nss' '''
get()
语法:字典序列.get(key,默认值)
注意:如果当前查找的key不存在则返回第二个参数,如果省略第二个参数,则返回None
dict1 = {'name':'张山', 'age':18, 'sex':'男'} print(dict1.get('name')) print(dict1.get('name',13)) print(dict1.get('nae')) print(dict1.get('nae',12)) ''' 输出: 张山 张山 None 12 '''
keys()和values()
dict1 = {'name':'张山', 'age':18, 'sex':'男'} print(dict1.keys()) print(dict1.values()) ''' dict_keys(['name', 'age', 'sex']) dict_values(['张山', 18, '男']) '''
查找字典中所有的key或value,返回一个可迭代对象
items()
dict1 = {'name':'张山', 'age':18, 'sex':'男'} print(dict1.items()) ''' 输出: dict_items([('name', '张山'), ('age', 18), ('sex', '男')]) '''
查找字典中所有的键值对,返回可迭代对象,里面的数据是元组,元组数据1是字典的key,元组数据2是字典的key对应的值
6.循环遍历
遍历字典的key
dict1 = {'name':'张山', 'age':18, 'sex':'男'} for i in dict1.keys(): print(i) ''' 输出: name age sex '''
遍历字典的value
dict1 = {'name':'张山', 'age':18, 'sex':'男'} for i in dict1.values(): print(i) ''' 输出: 张山 18 男 '''
遍历字典的元素
dict1 = {'name':'张山', 'age':18, 'sex':'男'} for item in dict1.items(): print(item) ''' 输出: ('name', '张山') ('age', 18) ('sex', '男') '''
遍历字典的键值对(对键值对拆包)
dict1 = {'name':'张山', 'age':18, 'sex':'男'} for key,value in dict1.items(): print(f'{key}:{value}') ''' 输出: name:张山 age:18 sex:男 '''
二、集合
创建集合使用set()或{},如果要创建空集合只能用set(),因为{}用来创建空字典了
集合是无序的,所以不支持下标操作,且有去重功能
set1 = {10, 20, 30, 40} print(set1) print(type(set1)) set2 = set('abcdf') print(set2) print(type(set2)) set3 = {10, 10, 2, 2, 30, 30, 4, 4, 5} print(set3) print(type(set3)) ''' 输出: {40, 10, 20, 30} <class 'set'> {'d', 'b', 'c', 'a', 'f'} <class 'set'> {2, 4, 5, 10, 30} <class 'set'> '''
注意:用set()创建字典,只能以一个字符串的形式来创建
set1 = set('abcdef') print(set1) print(type(set1)) set2 = set('abcdef','def','edf') print(set2) print(type(set2)) ''' 输出: {'e', 'f', 'd', 'a', 'b', 'c'} <class 'set'> Traceback (most recent call last): File "C:/Users/as_sc/PycharmProjects/untitled/hm-python-day6/02-集合.py", line 26, in <module> set2 = set('abcdef','def','edf') TypeError: set expected at most 1 argument, got 3 '''
创建空集合,只能用set()
set1 = set() print(set1) print(type(set1)) set2 = {} print(set2) print(type(set2)) ''' 输出: set() <class 'set'> {} <class 'dict'> '''
集合常见操作方法
增加
因为集合有去重功能,所以如果集合中存在要添加的数据,则不会进行任何操作
add() 用来追加一个数据,追加序列会报错
set1 = {'10','34'} set1.add('10') print(set1) set2 = {'12', '23'} set2.add('121') print(set2) ''' 输出: {'34', '10'} {'23', '121', '12'} '''
update() 用来追加一个序列,追加单一数据会报错
set1 = {'ss', '24'} set1.update(['se', 'ef']) print(set1) ''' 输出: {'ef', '24', 'se', 'ss'} '''
remove():删除集合中指定数据,如果数据不存在,则报错
set1 = {'rf', 'ds', 30} set1.remove('ds') print(set1) set2 = {'ss', 'oj', 98} set2.remove(100) print(set2) #报错 ''' 输出: Traceback (most recent call last): File "C:/Users/as_sc/PycharmProjects/untitled/hm-python-day6/02-集合.py", line 77, in <module> set2.remove(100) KeyError: 100 {'rf', 30} '''
discard():删除指定数据,数据不存在也不会报错
set1 = {'rf', 'ds', 30} set1.discard('ds') print(set1) set2 = {'ss', 'oj', 98} set2.discard(100) print(set2) ''' 输出: {'rf', 30} {'ss', 'oj', 98} '''
pop():随机删除某个数据,并返回该数据
set1 = {'rf', 'ds', 30} a = set1.pop() print(a) print(set1) set2 = {'ss', 'oj', 98} b = set2.pop() print(b) print(set2) ''' 输出: 第一次输出 ds {'rf', 30} oj {98, 'ss'} 第二次输出: ds {'rf', 30} 98 {'ss', 'oj'} '''
查找数据
in:判断数据是否在集合中,在返回true,反之false
not in:判断数据是否不在集合中,不在返回true,反之false
set1 = {'rf', 'ds', 30} print('rf' in set1) print('ra' in set1) print('ra' not in set1) print('rf' not in set1) ''' 输出: True False True False '''