2021-10-08 python从入门到精通--第六章 字典和集合

字典和集合

字典

字典的创建和删除

dictionary = { ‘key1’:‘value1’, ‘key2’:‘value2’, ‘key3’:‘value3’,}

空字典
dictionary = {}
dictionary = dict()

1.通过映射函数创建字典
dict()除了可以创建空字典,还可以通过已有数据快速创建字典
dictionary = dict(zip(list1, list2))

>>> list1 = ['1', '2', '3']
>>> list2 = ['a', 'b', 'c']
>>> dic = dict(zip(list1, list2))
>>> dic
{'1': 'a', '2': 'b', '3': 'c'}

2.通过给定键值创建字典
dictionary = dict(key1=value1,key2=value2…)
dictionary = dict(‘1’=‘a’,‘2’=‘b’,…)

dictionary = dict.fromkeys(list1)

创建仅包括名字的字典

>>> dic = dict.fromkeys(list1)
>>> dic
{'1': None, '2': None, '3': None}
通过已存在的元组和列表创建字典
>>> tuple1 = 'a','b','c'
>>> dic = {tuple1:list1}
>>> dic
{('a', 'b', 'c'): ['1', '2', '3']}
删除字典

del dictionary
dictionary.clear() #删除字典中的所有元素
dictionary.pop(‘1’)

>>> dic.pop('1')
'a'
>>> dic
{'2': 'b', '3': 'c'}

dictionary.popitem()

>>> i = dic.popitem()
>>> i
('3', 'c')
>>> dic
{'1': 'a', '2': 'b'}
>>> i = dic.popitem()
>>> i
('2', 'b')
>>> dic
{'1': 'a'}

访问字典

通过键获取值:

>>> dic =  {'1': 'a', '2': 'b', '3': 'c'}
>>> i = dic['1']
>>> i
'a'

通过get()获取值:
dictionary.get(key[,default])
default是键不存在时的返回值,默认为None

>>> dic.get('1','fuck')
'a'
>>> dic.get('d','fuck')
'fuck'

遍历字典

dictionary.items()
返回值为元组形式的键值对列表

>>> for item in dic.items():
...     print(item)
...
('1', 'a')
('2', 'b')
('3', 'c')
>>> for key,value in dic.items():
...     print(key,value)
...
1 a
2 b
3 c

添加/修改/删除字典元素

dictionary[key] = value

>>> i = {'1':'a','2':'b','3':'c'}
>>> i['4'] = 'd'
>>> i
{'1': 'a', '2': 'b', '3': 'c', '4': 'd'}
>>> i['2'] = 'e'
>>> i
{'1': 'a', '2': 'e', '3': 'c', '4': 'd'}
>>> del i['3']
>>> i
{'1': 'a', '2': 'e', '4': 'd'}
>>> if '1' in i:
...     del i['1']
...
>>> i
{'2': 'e', '4': 'd'}

字典推导式

>>> import random
>>> randomdict = {i:random.randint(10,100) for i in range(1,5)}
>>> randomdict
{1: 68, 2: 21, 3: 92, 4: 22}
>>>
>>> m = ('1','2','3')
>>> n = ['a','b','c']
>>> dict = {i:j for i,j in zip(m,n)}
>>> dict
{'1': 'a', '2': 'b', '3': 'c'}

集合

创建集合

使用{}创建

集合内的元素是唯一的,如果重复,会自动保留一个
集合内的元素每次输出时排列顺序可能不同

>>> i = {4,3,1,2,3,5,4}
>>> i
{1, 2, 3, 4, 5}
使用set()函数创建

setname = set(iteration)
iteration可以是列表,元组,字符串,range对象

>>> set1 = set("abcdefg")
>>> set1
{'d', 'f', 'a', 'b', 'g', 'e', 'c'}
>>> set2 = set([1,3,6,4,8,5])
>>> set2
{1, 3, 4, 5, 6, 8}
>>> set3 =set(('a','b','c','d'))
>>> set3
{'b', 'c', 'd', 'a'}

只能使用set()创建空集合,不能使用{},直接使用{}表示创建一个空字典

添加/删除元素

setname.add(element)
element只能时字符串,数字,布尔值,不能使用列表,元组等

>>> set1 = set("abcdefg")
>>> set1.add(1)
>>> set1
{1, 'd', 'f', 'a', 'b', 'g', 'e', 'c'}
>>> set1.add('zxc')
>>> set1
{1, 'd', 'f', 'a', 'zxc', 'b', 'g', 'e', 'c'}

del删除整个集合
pop()/remove()方法删除一个元素
clear()清空集合

>>> set1 = set("abcdefg")
>>> set1.pop()
'd'
>>> set1
{'f', 'a', 'b', 'g', 'e', 'c'}
> set1.remove('f')
>>> set1
{'a', 'b', 'g', 'e', 'c'}
>>> set1.clear()
>>> set1
set()

交集/并集/差集

交集:&
并集:|
差集:-
对称差集:^

>>> set1 = set("abcdefg")
>>> set2 = set("1234adf")
>>> set1 & set2
{'a', 'f', 'd'}
>>> set1 | set2
{'d', 'f', '1', 'a', 'b', 'g', '2', '3', 'e', 'c', '4'}
>>> set1 - set2
{'b', 'c', 'g', 'e'}
>>> set1 ^ set2
{'1', 'b', 'g', '3', 'e', '2', 'c', '4'}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值