Python学习7--字典和集合

一、字典

1、字典的定义

字典存储以键–值对形式存储数据
字典的原理:
建立哈希表(散列表),能够根据键值对key --value直接进行访问
字典的key会使用hash函数映射成一个hashcode(整数),hashcode指向的是值的地址。如果出现映射地址冲突的情况,会将映射到的对应value地址值往后顺延,直到地址值不再被占用冲突。

字典格式:
字典名={key1:value1,key2:value2…}

字典的特点:
(1)字典中的键值对是无序的
(2)字典中的key不能重复
(3)字典中的key必须是不可变类型

t1 = {"tom":80,"jerry":86,"kate":91}
t2 = dict(tom = 81,jerry=87,kate = 92)
t3 = {}
print(t1,type(t1))
print(t2,type(t2))
print(t3,type(t3))

输出:

{'tom': 80, 'jerry': 86, 'kate': 91} <class 'dict'>
{'tom': 81, 'jerry': 87, 'kate': 92} <class 'dict'>
{} <class 'dict'>

2、字典的key和value的访问

1)访问字典
访问字典不能使用索引,因为无序
访问字典格式为:字典[key] —value
格式: 字典名[key]

t1 = {"tom":80,"jerry":86,"kate":91}
print(t1["tom"])

输出:

80

2)修改字典
格式:字典[key]=赋值方式
如果key已经存在,则修改value,如果不存在,则新创建键值对

t1 = {"tom":80,"jerry":86,"kate":91}
t1["tom"]=101
print(t1)
t1["lily"]=99
print(t1)

输出:

{'tom': 101, 'jerry': 86, 'kate': 91}
{'tom': 101, 'jerry': 86, 'kate': 91, 'lily': 99}

3)定义字典的时候如果出现重复的key,会使用最后一个赋值将之前的key-value覆盖

t1 = {"tom":80,"jerry":86,"kate":91,"kate":88}
print(t1)

输出:

{'tom': 80, 'jerry': 86, 'kate': 88}

4)在字典中key要求是不可变类型(字符串、数值、元组(所有元素必须是不可变类型))
如:

a=[1,2,3]
s={a:"dd"}

会报错:

TypeError: unhashable type: 'list'

3、字典的操作:运算符

1)字典不支持 + * > <
如:

print(s+{"lucky":88})

会报错:

TypeError: unhashable type: 'list'

2)is == in

t1 = {"tom":80,"jerry":86,"kate":91}
t2 = {"tom":80,"jerry":86,"kate":91}
t = t1
print(id(t1),id(t2),id(t))
print(id(t1["tom"]),id(t2["tom"]),id(t["tom"]))

输出:

2550807485824 2550807485896 2550807485824
1960146464 1960146464 1960146464

3)in not in : key是否存在在字典中(只能判断key、不能直接判断value)。
如:

t1 = {"tom":80,"jerry":86,"kate":91}
print("tom" in t1)
print(80 in t1)

输出:

True
False

4、字典的相关方法

1)追加相关
a)创建类别(fromkeys)
fromkeys:使用序列中的每一个元素作为key生成字典,value使用统一的值,并且是新创建的对象
如:

d = {}
print(d.fromkeys((1,2,3)))
print(d.fromkeys([1,2,3]))
print(d.fromkeys((1,2,3),"hello"))
print(d.fromkeys((1,2,3),("hi","hello","nice")))

输出:

{1: None, 2: None, 3: None}
{1: None, 2: None, 3: None}
{1: 'hello', 2: 'hello', 3: 'hello'}
{1: ('hi', 'hello', 'nice'), 2: ('hi', 'hello', 'nice'), 3: ('hi', 'hello', 'nice')}

b)增加类别(setdefault(key,value))
功能:给字典进行追加键值对
key键 value指定的值 如果没有value,会追加key键 None值的键值对
如果key已存在,则不改变value
如果key不存在,则按照key,value进行追加

d= {}
d = d.fromkeys((1,2,3),"one")
print(d)
d.setdefault(4)
print(d)
d.setdefault(5,"two")
print(d)
d.setdefault(5,"three")
print(d)

输出:

{1: 'one', 2: 'one', 3: 'one'}
{1: 'one', 2: 'one', 3: 'one', 4: None}
{1: 'one', 2: 'one', 3: 'one', 4: None, 5: 'two'}
{1: 'one', 2: 'one', 3: 'one', 4: None, 5: 'two'}

c)update: 字典追加key和value,一次性追加多个键值对
update:对已有key进行更新,对不存在的key进行追加

d={}
d = d.fromkeys((1,2,3),"one")
print(d)
b = {3:"hi",4:"two",5:"three"}
d.update(b)
print(d)

输出:

{1: 'one', 2: 'one', 3: 'one'}
{1: 'one', 2: 'one', 3: 'hi', 4: 'two', 5: 'three'}

2)删除相关
a)pop: 删除指定的键值对,返回被删除键值对的值
如:

b = {4:"php",5:"python",6:"new"}
print(b.pop(6))
print(b)

输出:

new
{4: 'php', 5: 'python'}

b)popitem:随机删除一个键值对,返回被删除的键值对

b = {4:"php",5:"python",6:"new"}
print(b.popitem(),b)

输出:

(6, 'new') {4: 'php', 5: 'python'}

c) clear清空字典

b = {4:"php",5:"python",6:"new"}
print(b)
b.clear()
print(len(b),b)

输出:

{4: 'php', 5: 'python', 6: 'new'}
0 {}

3)获取相关
get(key)key如果不存在,不会报错,输出None
当key不存在时,添加的报错信息会显示出来

b = {4:"php",5:"python",6:"new"}
print(b.get(4))
print(b.get(9))
print(b.get(4,"不存在4"))
print(b.get(9,"不存在9"))

输出:

php
None
php
不存在9

5、字典的遍历

a)返回所有的键

d = {"one":1,"two":2,"three":3}
for k in d.keys():
    print(k)

输出:

one
two
three

b)返回所有的值

d = {"one":1,"two":2,"three":3}
for v in d.values():
    print(v)

输出:

1
2
3

c)、返回键值对

d = {"one":1,"two":2,"three":3}
for k,v in d.items():
    print(k,v)

d = {"one":1,"two":2,"three":3}
for k in d.keys():
    print(k,d[k])
    

输出:

one 1
two 2
three 3

6、字典推导式

格式:{输出表达式 for k,v in 字典}

b = {4:"php",5:"python",6:"new"}
print({k**2:v for k,v in b.items() if k>4})

输出:

{25: 'python', 36: 'new'}

二、集合Set

集合跟字典相比,就像只存储了字典中的key。
特点:
(1)不能重复
(2)元素是不可变类型
(3)无序
一定注意,集合不能使用索引去获取元素

1、集合的创建

格式:集合名={元素1,元素2…}
1)集合是无序的,输出的时候也是无序的(如上输出)

s = {"a","b","c"}
print(s,type(s))

输出:

{'a', 'c', 'b'} <class 'set'>

2)直接{}创建的是空字典,不是空集合,创建空集合只能使用set

d = {}
print(d,type(d))

s = set()
print(s,type(s))

输出:

d = {}
print(d,type(d))

s = set()
print(s,type(s))

3)如果包含重复元素,会去掉重复的元素

s = {"a","b","c","a","b"}
print(s,type(s))

输出:

{'b', 'a', 'c'} <class 'set'>

4)集合中的元素必须是不可变类型

s = {"a","b","c",[1,23]}
print(s)

输出:

TypeError: unhashable type: 'list'

5)集合相当于是值为None的字典

t = {}
print(t)
print(t.fromkeys((1,2,3)))

输出:

{}
{1: None, 2: None, 3: None}

2、集合的运算

1)运算符 不支持+ *
支持== is in

s1 = {1,2,3}
s2 = {1,2,3}
s3 = s1
print(id(s1),id(s2),id(s3))
print(1 in s1)
print(s1 == s2)
print(s1 is s2,s1 is s3)

输出:

3069159949032 3069162423240 3069159949032
True
True
False True

2)差集- 、并集| 、交集&、相对差集^

在这里插入图片描述

a)差集-
A有而B没有的部分

a = {"a","b","c","d","e","h"}
b = {"a","b","f","g","h"}
print(a-b)

输出:

{'e', 'd', 'c'}

b)并集|
涵盖A、B所有的元素

a = {"a","b","c","d","e","h"}
b = {"a","b","f","g","h"}
print(a|b)

输出:

{'e', 'g', 'd', 'h', 'b', 'f', 'c', 'a'}

c)交集&
A和B均有的元素

a = {"a","b","c","d","e","h"}
b = {"a","b","f","g","h"}
print(a&b)

输出:

{'h', 'b', 'a'}

d)相对差集 ^
两个集合不同时拥有的元素

a = {"a","b","c","d","e","h"}
b = {"a","b","f","g","h"}
print(a^b)

输出:

{'e', 'f', 'g', 'd', 'c'}

3)< > <= >=表示集合的父子集关系

a = {"a","b","c",}
b = {"a","b","c","d","e"}
c ={"a","b","d","e"}
print(a<b)
print(a<c)

输出:

True
False

4) 集合的相关方法
a)添加add

a = {"a","b","c",}
a.add("d")
print(a)

输出:

{'a', 'd', 'c', 'b'}

b)删除remove 删除指定内容的元素,如果不存在会报错
discard删除指定内容的元素,不存在时不会报错

a = {"a","b","c"}
a.remove("c")
print(a)
# a.remove("aa")   报错KeyError: 'aa'
a.discard("aa")

输出:

{'b', 'a'}

随机删除一个元素,返回被删除的元素

a = {"a","b","c"}
print(a.pop(),a)

输出:

b {'a', 'c'}

c)复制类
copy的对象不是原来的对象,是新创建的
(跟元组不同,虽然都是不可变的数据类型,但是集合属于元素不可变,不是集合本身不可变)

a = {"a","b","c"}
b = a.copy()
print(id(a),id(b))

输出:

2095030944488 2095033418696

d)交集、并集、差集、相对差集
1.差集defference:返回在当前集合中存在,但不在参数集合中的元素

a = {"a","b","c","d"}
b = {"a","b","e","f"}
print(a-b)
print(a.difference(b))
print(a)

输出:

{'d', 'c'}
{'d', 'c'}
{'d', 'c', 'b', 'a'}

带update的代表原地修改

a = {"a","b","c","d"}
b = {"a","b","e","f"}
print(a.difference_update(b))
print(a)

输出:

None
{'d', 'c'}

2.交集intersection
两个集合均有的元素

a = {"a","b","c","d"}
b = {"a","b","e","f"}
print(a&b)
print(a.intersection(b))
print(a)
print(a.intersection_update(b))
print(a)

输出:

{'b', 'a'}
{'b', 'a'}
{'c', 'b', 'a', 'd'}
None
{'b', 'a'}

3.并集union
包含两个集合所有元素
在原地更新时不时union_update时,而是update

a = {"a","b","c","d"}
b = {"a","b","e","f"}
print(a|b)
print(a.union(b))
print(a)
print(a.update(b))
print(a)

输出:

{'f', 'a', 'd', 'c', 'e', 'b'}
{'f', 'a', 'd', 'c', 'e', 'b'}
{'a', 'b', 'd', 'c'}
None
{'f', 'a', 'd', 'c', 'e', 'b'}

4.对称差集ymmetric_difference
两个集合不同时拥有的元素

a = {"a","b","c","d"}
b = {"a","b","e","f"}
print(a^b)
print(a.symmetric_difference(b))
print(a)
print(a.symmetric_difference_update(b))
print(a)

输出:

{'d', 'f', 'c', 'e'}
{'d', 'f', 'c', 'e'}
{'d', 'c', 'b', 'a'}
None
{'d', 'f', 'c', 'e'}

5.判断交集是否为空isdisjoint

a = {"a","b","c","d"}
b = {"a","b","e","f"}
print(a.isdisjoint(b))

输出:

False

6.判断当前是否为参数集合的子集issubset

a = {"a","b"}
b = {"a","b","e","f"}
print(a.issubset(b))

输出:

True

7.判断当前是否为参数集合的父集issuperset

a = {"a","b","c","d","e","f"}
b = {"a","b","e","f"}
print(a.issuperset(b))

输出:

True

3、集合的遍历

s = {1,2,3}
for i in s:
    print(i)

输出:

1
2
3

4、集合推导式

a = {1,2,3,4}
b = {"a","b","c"}
print({i**2 for i in a},"集合推导式")
print({i+" new" for i in b},"集合推导式")

输出:

{16, 1, 4, 9} 集合推导式
{'a new', 'c new', 'b new'} 集合推导式

三、数据转换

列表 list
元组 tuple
字典 dict
集合 set

s="abc"
t=(1,2,3)
se={1,2,3}
print(list(s))
print(tuple(s))
print(set(s))
print(dict(a=1,b=2,c=3))
print("======================")
print(list(t))
print(set(t))
print("======================")
print(list(se))
print(tuple(se))
print("======================")
li=[1,2,3,3]
t=(1,2,3,3)
print(set(li))
print(set(t))

输出:

['a', 'b', 'c']
('a', 'b', 'c')
{'b', 'c', 'a'}
{'a': 1, 'b': 2, 'c': 3}
======================
[1, 2, 3]
{1, 2, 3}
======================
[1, 2, 3]
(1, 2, 3)
======================
{1, 2, 3}
{1, 2, 3}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值