0x07 - Python中的字典和集合


目录: 0x00 - Python学习笔记

字典

当索引不好用时,就有了字典的诞生,他是Python中唯一一个映射类型,用键值对来表示的,一个 键值对 就叫做一个 ,有的地方也称关联数组
字典中是没有顺序的,它会自动按照字符优先级储存
像列表、元组、字符串都属于 映射类型 ,他的索引(0,1,2,…)与内容无关

  • 创建
    Python中用大括号来表示一个字典
>>> dict1 = {'img':'1.png', 'key':'yes', 'path':'flag'}
>>> dict2 = dict((('img','1.png'),('key','yes'),('path','flag')))
>>> dict3 = dict(img='1.png', key='yes', path='flag')
>>> dict4 = {}
  • 调用
>>> print(dict1['path'])
flag
  • 添加
    在序列中为不存在的索引赋值会直接报错,但在字典中为不存在的键赋值会自动添加这个键值对(如果存在则修改)
>>> dict4 = {}
>>> dict4['web'] = 'dog!!!'
>>> dict4
{'web': 'dog!!!'}
  • 工厂函数
    工厂函数就是可以类似于工厂创建一个类型的函数
    他们有:str()int()list()tuple()dict()

内建方法

  • fromkeys(s[,v])
    这个方法 创建 并返回一个新的字典,第一个参数时键,第二个参数是值(可省略)
    它创建的字典的值都是一模一样的
>>> dict1 = {}
>>> dict1.fromkeys((1,2,3))
{1: None, 2: None, 3: None}
>>> dict1.fromkeys((1,2,3), (1,'asd'))
{1: (1, 'asd'), 2: (1, 'asd'), 3: (1, 'asd')}
  • keys()
    返回键
>>> dict2 = dict((('img','1.png'),('key','yes'),('path','flag')))
>>> dict2.keys()
dict_keys(['img', 'key', 'path'])
  • items()
    返回像
>>> dict2 = dict((('img','1.png'),('key','yes'),('path','flag')))
>>> dict2.items()
dict_items([('img', '1.png'), ('key', 'yes'), ('path', 'flag')])
  • get()
    用索引键的方式访问一个值,如果该键不存在也不会报错(主要用于给用户)
  • clear()
    清空一个字典
>>> dict2 = dict((('img','1.png'),('key','yes'),('path','flag')))
>>> dict2.clear()
>>> dict2
{}

可以用赋空字典的方式,但其不严谨

>>> dict2 = dict((('img','1.png'),('key','yes'),('path','flag')))
>>> dict3 = dict2
>>> dict2 = {}
>>> dict2
{}
>>> dict3
{'img': '1.png', 'key': 'yes', 'path': 'flag'}
  • copy()
    浅拷贝

赋值是直接贴了个标签在数值上,可以理解为又起了一个名字
而浅拷贝是拷贝出一份新的数据

>>> dict2 = dict((('img','1.png'),('key','yes'),('path','flag')))
>>> dict3 = dict2
>>> dict4 = dict2.copy()
>>> id(dict2)
1918018041472
>>> id(dict3)
1918018041472
# 可以看到上面两个是一样的,只是名字不同
>>> id(dict4)
1918018684288
  • pop()
    弹出一个值,其他值保留
>>> dict1 = {1:'one', 2:'two', 3:'three', 4:'four'}
>>> dict1.pop(1)
'one'
>>> dict1
{2: 'two', 3: 'three', 4: 'four'}
  • popitem()
    随机弹出一个值
>>> dict1 = {1:'one', 2:'two', 3:'three', 4:'four'}
>>> dict1.popitem()
(4, 'four')
>>> dict1
{1: 'one', 2: 'two', 3: 'three'}
  • update()
    用字典的映射关系更新一个字典
>>> dict1 = {1:'one', 2:'two', 3:'three', 4:'four'}
>>> a = {1:'asdasdasd'}
>>> dict1.update(a)
>>> dict1
{1: 'asdasdasd', 2: 'two', 3: 'three', 4: 'four'}

集合

集合也是用大括号来表示的

>>> set1 = {1,2,3,4,5,6}
>>> type(set1)
<class 'set'>

集合满足无序性、互异性

  • 创建
    直接使用大括号创建
    使用 set() 工厂函数创建
>>> set1 = set((1,2,3,4,5,6,8,5,2,1,2,4,2,2))
>>> set1
{1, 2, 3, 4, 5, 6, 8}
  • 去重
>>> list1 = [1,2,1,2,4,4,5,6,3,3,2,1,1]
>>> list1 = list(set(list1))
>>> list1
[1, 2, 3, 4, 5, 6]
  • 判断
    可以使用 for 将集合中元素一个一个取出来,也可以使用innot in进行判断
>>> set1 = set((1,2,3,4,5,6,8,5,2,1,2,4,2,2))
>>> 2 in set1
True
>>> '2' in set1
False
  • 增加和移除
>>> set1 = set((1,2,3,4,5,6,8,5,2,1,2,4,2,2))
>>> set1
{1, 2, 3, 4, 5, 6, 8}
>>> set1.add(10)
>>> set1
{1, 2, 3, 4, 5, 6, 8, 10}
>>> set1.remove(1)
>>> set1
{2, 3, 4, 5, 6, 8, 10}
  • frozen:冰冻的
    不可变集合:不可改变其中数据
>>> set1 = frozenset((1,2,3,4))
>>> set.add(1)
Traceback (most recent call last):
  File "<pyshell#64>", line 1, in <module>
    set.add(1)
TypeError: descriptor 'add' for 'set' objects doesn't apply to a 'int' object
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1ta-chi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值