点我
字典dict与集合set
字典dict
字典是包含若干“键:值”元素的无序可变序列
字典的每一个元素包含用冒号分割开的“键” 和 “值” 两部分,不同元素之间用逗号隔开,所有元素放在{ }中,表示一种映射或对应关系,也称关联数组
字典的键可以是python中任意不可变数据类型,如:整数、实数、复数、字符串、元组等,不能使用列表、集合、字典或其他可变类型作为字典的键
字典的键不允许重复,值可以重复
字典的创建
通过{}或者dict()创建
>>> a = {'id':1,'name':'make','age':20}
>>> a
{'id': 1, 'name': 'make', 'age': 20}
>>> b = dict(id=1,name='张山',age=22)
>>> b
{'id': 1, 'name': '张山', 'age': 22}
>>> type(a)
<class 'dict'>
>>> type(b)
<class 'dict'>
字典的删除
del 字典名 删除整个字典
字典名.clear() 清除字典中的数据
>>> a
{'id': 1, 'name': 'make', 'age': 20}
>>> b
{'id': 1, 'name': '张山', 'age': 22}
>>> a.clear()
>>> a
{}
>>> del b
>>> b
Traceback (most recent call last):
File "<pyshell#105>", line 1, in <module>
b
NameError: name 'b' is not defined
- del 字典名 直接删除字典
- pop() 删除指定键对应的值,并返回该值;如果没有指定的键,允许返回指定的内容
- popitem() 删除字典中最后一对键值对,并返回删除的元素;如果为空字典,则抛出异常
>>> b
{'id': 1, 'name': 'wang'}
>>> del b
>>> b
Traceback (most recent call last):
File "<pyshell#157>", line 1, in <module>
b
NameError: name 'b' is not defined
>>> a
{'name': '张山', 'age': 20, 'id': 1, 'add': '北京'}
>>> a.pop('age')
20
>>> a
{'name': '张山', 'id': 1, 'add': '北京'}
>>> a.pop('app','没有该键')
'没有该键'
>>> a.popitem()
('add', '北京')
>>> a
{'name': '张山', 'id': 1}
>>> a.popitem()
('id', 1)
>>> a
>>> a
{'name': '张山'}
>>> a.popitem()
('name', '张山')
>>> a
{}
>>> a.popitem()
Traceback (most recent call last):
File "<pyshell#189>", line 1, in <module>
a.popitem()
KeyError: 'popitem(): dictionary is empty'
字典元素的访问
字典中的每一个元素表示一种映射关系或对应关系,根据提供的“键”作为下标就可以访问“键”对应的“值”,如果不存在则会抛出异常
- 通过“键”作为下标,访问对应的值
- 使用get()方法,返回指定键的值,可指定查找的键不存在时,返回特定的内容
- 可以使用for循环对字典进行遍历,默认遍历字典中的“键”
- items() 显示字典中的元素
- values() 显示字典中的“值”
>>> a
{'id': 1, 'name': '张山', 'age': 20}
>>> a['id']
1
>>> a['add']
Traceback (most recent call last):
File "<pyshell#114>", line 1, in <module>
a['add']
KeyError: 'add'
>>> a.get('name')
'张山'
>>> a.get('add','该键不存在')
'该键不存在'
>>> for i in a:
print(i)
id
name
age
>>> a
{'id': 1, 'name': '张山', 'age': 20}
>>> for i in a.items():
print(i)
('id', 1)
('name', '张山')
('age', 20)
>>> for i in a.values():
print(i)
1
张山
20
>>>
>>> a
{'id': 1, 'name': '张山', 'age': 20}
>>> a.keys() //查看字典中的键
dict_keys(['id', 'name', 'age'])
>>> a.values() //查看字典中的值
dict_values([1, '张山', 20])
>>> a.items() //查看字典中的元素
dict_items([('id', 1), ('name', '张山'), ('age', 20)])
字典的添加与修改
以指定“键”为下标
- 若该“键”存在时,则表示修改该“键”对应的值
- 若该“键”不存在时,则表示添加一个新的“键:值”对,也就是为字典添加新的元素
- 使用update() 将另一个字典中的键值对,一次性添加到当前字典对象中,若两个字典的键相同,则以另一个字典的键对应的值为准
>>> a
{'id': 1, 'name': '张山', 'age': 20}
>>> a['id'] = 2
>>> a
{'id': 2, 'name': '张山', 'age': 20}
>>> a['add'] = '北京'
>>> a
{'id': 2, 'name': '张山', 'age': 20, 'add': '北京'}
>>> a
{'id': 2, 'name': '张山', 'age': 20, 'add': '北京'}
>>> a.update({'id':4,'sex':'nan'})
>>> a
{'id': 4, 'name': '张山', 'age': 20, 'add': '北京', 'sex': 'nan'}
>>> b = {'id':10,'like':'ABC'}
>>> b
{'id': 10, 'like': 'ABC'}
>>> a
{'id': 4, 'name': '张山', 'age': 20, 'add': '北京', 'sex': 'nan'}
>>> a.update(b)
>>> a
{'id': 10, 'name': '张山', 'age': 20, 'add': '北京', 'sex': 'nan', 'like': 'ABC'}
集合set
- 集合无序可变序列,使用{}作为定界符,元素之间使用逗号隔开
- 同一集合中内的每一个元素都是唯一的,不允许重复
- 集合只能包含 数字、字符串、元组等不可变数据类型,不能包括列表、字典、集合等可变数据类型
集合的创建
通过{}或者set创建(无法创建空集合)
>>> a = {1,2,3,'a'}
>>> a
{1, 2, 3, 'a'}
>>> type(a)
<class 'set'>
>>> a = {}
>>> type(a)
<class 'dict'>
>>> a = set((3,4,5))
>>> a
{3, 4, 5}
>>> type(a)
<class 'set'>
集合的添加
- add() 添加新的元素,如需要添加的元素已存在则忽略该操作,不会抛出异常
- update() 合并另外一个集合中的元素到当前集合中,自动去重
>>> b
{1, 2, 3}
>>> b.add(4)
>>> b
{1, 2, 3, 4}
>>> c = {5,6,7,2}
>>> c
{2, 5, 6, 7}
>>> b.update(c)
>>> b
{1, 2, 3, 4, 5, 6, 7}
集合的删除
- del 集合名 直接删除该集合
- clear() 清除集合中的数据
- pop() 随机删除并返回删除的元素,如果集合为空则抛出异常
- remove() 删除集合中指定元素,不会返回被删除的元素,指定元素不存在则抛出异常
- discard() 从集合中删除特定元素,如元素不存在时则忽略该操作
>>> a
{3, 4, 5, 7, (5, 6)}
>>> del a
>>> a
Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
a
NameError: name 'a' is not defined
>>> b
{1, 2, 3, 4, 5, 6, 7}
>>> b.pop()
1
>>> b
{2, 3, 4, 5, 6, 7}
>>> b.pop()
2
>>> b
{3, 4, 5, 6, 7}
>>> b.remove(5)
>>> b
{3, 4, 6, 7}
>>> b.remove()
Traceback (most recent call last):
File "<pyshell#46>", line 1, in <module>
b.remove()
TypeError: remove() takes exactly one argument (0 given)
>>> b
{3, 4, 6, 7}
>>> b.discard(3)
>>> b
{4, 6, 7}
>>> b.discard(1)
>>> b
{4, 6, 7}
集合的运算
方法 | 描述 |
---|---|
len() | 返回集合中的元素个数 |
copy() | 拷贝一个集合 |
difference() | 返回多个集合的差集 |
difference_update() | 移除集合中的元素,该元素在指定的集合中也存在 |
intersection() | 返回集合的交集 |
intersection_update() | 返回集合的交集 |
isdisjoint() | 判断2个集合是否包含相同的元素,没有返回True,有返回False |
issubset() | 判断指定集合是否为该方法参数集合的子集 |
issuperset() | 判断该方法的参数集合是否为指定集合的子集 |
symmetric_difference() | 返回两个集合中不重复的元素集合 |
symmetric_difference_update() | 移除当前集合中在另外一个指定集合相同的元素,并将另外一个指定集合中不同的元素插入到当前集合中 |
union() | 返回两个集合的并集 |
符号运算
符号 | 说明 | |
---|---|---|
\ | 并集 | |
& | 交集 | |
- | 差集 | |
^ | 对称差集 | |
< | 真子集 | |
<= | 子集 |
>>> a = {1,2,3,4,5}
>>> b = {4,5,6,7,8}
>>> a | b
{1, 2, 3, 4, 5, 6, 7, 8}
>>> a & b
{4, 5}
>>> a - b
{1, 2, 3}
>>> b - a
{8, 6, 7}
>>> a ^ b
{1, 2, 3, 6, 7, 8}
>>> a < b
False
>>> c = {1,2,3,4,5,6,7}
>>> a < c
True
>>> a <= b
False
>>> a <= c
True
分类:
Python
版权申明
本文系作者 @小白学安全 原创发布在 xbxaq.com 站点,未经许可,禁止转载!
评论