python学习笔记字典与集合(Dictionary和set)(六)

字典 dict

特性

Python内置了字典:dict的支持,dict全称dictionary,在其他语言中也称为map,使用键-值(key-value)存储,具有极快的查找速度。
key-value存储方式,在放进去的时候,必须根据key算出value的存放位置,这样,取的时候才能根据key直接拿到value。
dict有以下几个特点:

  1. 查找和插入的速度极快,不会随着key的增加而增加;
  2. 需要占用大量的内存,内存浪费多。

要求

dict可以用在需要高速查找的很多地方,在Python代码中几乎无处不在,正确使用dict非常重要,需要牢记的第一条就是dict的key必须是不可变对象。
这是因为dict根据key来计算value的存储位置,如果每次计算相同的key得出的结果不同,那dict内部就完全混乱了。

用法

fromkeys方法

Python 字典 fromkeys() 函数用于创建一个新字典,以序列 seq 中元素做字典的键,value 为字典所有键对应的初始值。

语法

fromkeys()方法语法:

dict.fromkeys(seq[, value])
参数

seq – 字典键值列表。
value – 可选参数, 设置键序列(seq)的值。

返回值

该方法返回一个新字典。

示例
>>> dict1.fromkeys((1,2,3))
{1: None, 2: None, 3: None}
>>> dict1.fromkeys((1,2,3),'Number')
{1: 'Number', 2: 'Number', 3: 'Number'}
>>> dict2 = dict2.fromkeys(range(32),'赞')
>>> dict2
{0: '赞', 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: '赞'}

keys方法、values方法

获取字典dict的所有键,所有值

keys方法
>>> for eachKey in dict2.keys():
	print(eachKey)
0
1

...
values方法
>>> for eachValue in dict2.values():
	print(eachValue)
赞
赞

...

从dict中取值方法

1.根据key取value

>>> print(dict2[31])

2.判断dict中是否存在该键

>>> 32 in dict2
False
>>> 30 in dict2
True

3.取不到值时,赋予默认值

>>> dict2.get(32,'Null')
'Null'

dict的浅拷贝与深拷贝

通过等于号赋值时深拷贝,通过copy方法是浅拷贝;可通过查看字典的id值区分

>>> a = {'1':'one','2':'two','3':'three'}
>>> b = a.copy()
>>> c = a
>>> b
{'1': 'one', '2': 'two', '3': 'three'}
>>> a
{'1': 'one', '2': 'two', '3': 'three'}
>>> c
{'1': 'one', '2': 'two', '3': 'three'}
>>> id(a)
1777430372584
>>> id(b)
1777430372904
>>> id(c)
1777430372584
>>> c['4'] = 'four'
>>> a
{'1': 'one', '2': 'two', '3': 'three', '4': 'four'}
>>> c
{'1': 'one', '2': 'two', '3': 'three', '4': 'four'}

pop和popitem方法

1.pop()
If key is in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary
2.popitem()
Remove and return a (key, value) pair from the dictionary. Pairs are returned in LIFO order.

>>> a.pop('1')
'one'
>>> a
{'2': 'two', '3': 'three', '4': 'four'}
>>> a.popitem()
('4', 'four')
>>> a
{'2': 'two', '3': 'three'}

集合set

无序不重复元素集
set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。

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

示例:

给定一串数字,去除重复的

解法1:不使用set的解法

>>> num1 = [1,2,3,4,5,5,2,4,3,1]
>>> temp = []
>>> for each in num1:
	if each not in temp:
		temp.append(each)
		
>>> temp
[1, 2, 3, 4, 5]

解法2:使用set快速求解

>>>num1 = [1,2,3,4,5,5,2,4,3,1] 
>>>num1 = list(set(num1))
>>> num1
[1, 2, 3, 4, 5]

常用方法介绍

一.frozenset

不可变集合

>>> num2 = frozenset([1,2,3,4,5])
>>> num2.add(6)
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    num2.add(6)
AttributeError: 'frozenset' object has no attribute 'add'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值