Python数据类型三

数据进阶三

1.1散列类型(哈希类型)

1、数值类型: int float bool——》存储的是一个数值

2、序列类型: str list tuple——》存储多个数据

散列类型:字典、集合

①无序

②内部元素不重复

1.1.1字典 dict

用来保存一些典型的“对应关系”的数据类型

特点是用键值对的方式来存储数据

键值:类似于生活中的字典

key:value

核心区别,由于它是用键值对的方式进行存储数据,更适合去描述一个对象

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wp4TFMA0-1668779373752)(C:\Users\YiYouLi\AppData\Roaming\Typora\typora-user-images\1668762626790.png)]

dict={'车子':1, '李花花':2, "徐花花":3,"蓝胖子":4,'房子':5, '院子':6, '家人':7, '旺财':8}
user={"姓名":"蓝胖子","职业":"讲师"}
print(dict["蓝胖子"])
print(user["职业"])

4
讲师

Process finished with exit code 0

·字典是无序的,没有下标,是直接通过键值对的方式存储数据的

·不重复,字典的键(key)是不可以重复的,如果有重复的元素,是默认取最后一个元素

dict={'车子':1, '李花花':2, "蓝胖子":3,"蓝胖子":4,'房子':5, '蓝胖子':6, '家人':7, '旺财':8}
print(dict)

{'车子': 1, '李花花': 2, '蓝胖子': 6, '房子': 5, '家人': 7, '旺财': 8}

Process finished with exit code 0

·更改字典中的元素

·可变性,字典里面的数据是可变的

dict={'车子':1, '李花花':2, "蓝胖子":3,"蓝胖子":4,'房子':5, '蓝胖子':6, '家人':7, '旺财':8}
dict["房子"]=20
print(dict)

{'车子': 1, '李花花': 2, '蓝胖子': 6, '房子': 20, '家人': 7, '旺财': 8}

Process finished with exit code 0

当我没想要表示一组固定信息时,用字典可以更加直观地表示

字典包含多个key-value对

- 通过key访问value
- 通过key添加key-value:通过key添加新的key-value对到原有的字典中
- 通过key删除key-value:通过key删除key-value对	del
- 通过key修改key-value:通过key修改key-value对 新赋值的value会覆盖原有的value值
- 通过key判断key-value对是否存在:通过key判断key-value对是否存在in not in运算符

访问

scores={"语文":90}
print(scores["语文"])

90

Process finished with exit code 0

添加

scores={"语文":90}
scores["数学"]=95
scores[90]=100
print(scores)

{'语文': 90, '数学': 95, 90: 100}

Process finished with exit code 0

删除

scores={"语文":90}
scores["数学"]=95
scores[90]=100
print(scores)
del scores["语文"]
del scores[90]
print(scores)

{'语文': 90, '数学': 95, 90: 100}
{'数学': 95}

Process finished with exit code 0

修改

cars={"BWM":8.5,"BC":6.5,"DZ":7.5}
cars["BC"]=4.3
cars["DZ"]=10.2
print(cars)
{'BWM': 8.5, 'BC': 4.3, 'DZ': 10.2}

Process finished with exit code 0

判断

cars={"BWM":8.5,"BC":6.5,"DZ":7.5}
print("baoshijie" in cars)
print("bili" not in cars)

False
True

Process finished with exit code 0

字典的key是关键,key可以是任意不可变的类型

如果程序中要使用的字典的key都是整数类型,就可以考虑能否换成列表

1.1.2字典的常用的方法

可以通过dir(dict)方法来查看字典包含哪些类

print(dir(dict))
['__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ior__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__ror__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'items', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']
1.1.2.1 clear()

用于清空字典中所有的key-value对,对一个字典执行clear()之后,字典就会变成空字典

cars={"BWM":8.5,"BC":6.5,"DZ":7.5}
cars.clear()
print(cars)
print(cars.clear())

{}
None

Process finished with exit code 0

1.1.2.2 get()

根据key来获取value

cars={"BWM":8.5,"BC":6.5,"DZ":7.5}
print(cars.get("BWM"))

8.5

Process finished with exit code 0
1.1.2.3 update()

跟新已有的字典

cars={"BWM":8.5,"BC":6.5,"DZ":7.5}
cars.update({"liyiyou":5.20,"baoshijie":3.2})
cars["lanpangzi"]=6.0
print(cars)

{'BWM': 8.5, 'BC': 6.5, 'DZ': 7.5, 'liyiyou': 5.2, 'baoshijie': 3.2, 'lanpangzi': 6.0}

Process finished with exit code 0
1.1.2.4 items()、keys()、values()

dict_items:获取字典中所有的键值对

dict_keys:获取字典中所有的键值

dict_values:获取字典中所有的value值

cars={"BWM":8.5,"BC":6.5,"DZ":7.5,"liyiyou":5.20,"baoshijie":3.2}
print(cars.items())
im=cars.items()
print(type(im))

dict_items([('BWM', 8.5), ('BC', 6.5), ('DZ', 7.5), ('liyiyou', 5.2), ('baoshijie', 3.2)])
<class 'dict_items'>

Process finished with exit code 0
cars={"BWM":8.5,"BC":6.5,"DZ":7.5,"liyiyou":5.20,"baoshijie":3.2}
print(cars.items())
im=cars.items()
print(type(im))
print(cars.keys)
ip=cars.keys()
print(type(ip))
print(cars.values())
ik=cars.values()
print(type(ik))

dict_items([('BWM', 8.5), ('BC', 6.5), ('DZ', 7.5), ('liyiyou', 5.2), ('baoshijie', 3.2)])
<class 'dict_items'>
<built-in method keys of dict object at 0x0000017A69DDB1C0>
<class 'dict_keys'>
dict_values([8.5, 6.5, 7.5, 5.2, 3.2])
<class 'dict_values'>

Process finished with exit code 0
1.1.2.5 pop()方法
cars={"BWM":8.5,"BC":6.5,"DZ":7.5,"liyiyou":5.20,"baoshijie":3.2}
print(cars.pop("BC"))
print(cars)

6.5
{'BWM': 8.5, 'DZ': 7.5, 'liyiyou': 5.2, 'baoshijie': 3.2}

Process finished with exit code 0
1.1.2.6 setdefalut()方法

根据key获取对应的value值,额外功能就是当程序要获取key在字典中不存在时,这个方法会先为这个不存在的key设置一个默认的value,然后再返回这个key对应的value

cars={"BWM":8.5,"BC":6.5,"DZ":7.5,"liyiyou":5.20,"baoshijie":3.2}
print(cars.setdefault("baoshijie",3.5))
print(cars)

3.2
{'BWM': 8.5, 'BC': 6.5, 'DZ': 7.5, 'liyiyou': 5.2, 'baoshijie': 3.2}

Process finished with exit code 0

2.1集合

集合中的元素都是唯一的,不能重复,互不相同,放在一对{}中

{name1,name2,name3…}

只能存储不可变的数据类型(数字、字符串、元组),如果存储了可变类型的数据就会报错(列表、字典、集合)

print({[1,2,3]})

Traceback (most recent call last):
  File "E:\InPrg\ProPrg\Program Folder\code\test_python\1118.py", line 131, in <module>
    print({[1,2,3]})
TypeError: unhashable type: 'list'

Process finished with exit code 1
print(set={{1.2,3,4}})

Traceback (most recent call last):
  File "E:\InPrg\ProPrg\Program Folder\code\test_python\1118.py", line 133, in <module>
    print(set={{1.2,3,4}})
TypeError: unhashable type: 'set'

Process finished with exit code 1
print({{"BWM":2.0}})

Traceback (most recent call last):
  File "E:\InPrg\ProPrg\Program Folder\code\test_python\1118.py", line 133, in <module>
    print({{"BWM":2.0}})
TypeError: unhashable type: 'dict'

Process finished with exit code 1

集合去重,重复的元素只会保留一份数据

set={1,2,3,(4,5,6),"a","a",5,1,2}
print(set)

{1, 2, 3, 5, (4, 5, 6), 'a'}

Process finished with exit code 0

由于集合是无序的,所以不能通过下标来提取元素,会报错

创建空集合的时候必须使用set()而不能是{},因为{}默认是空字典

li=[]	#空列表
t=()	#空元组
str=""	#空字符串
dict={}	#空字典
set1=set{}	#空集合
li=[1,2,3,4,5,7,9,8,4,6,5,4,1,2,2,4,5,28,8,1,2,5,9,25,4,1]
li1=set(li)
print(li1)
print(type(li))

{1, 2, 3, 4, 5, 6, 7, 8, 9, 25, 28}
<class 'list'>

Process finished with exit code 0

2.1.1集合的数学运算

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Y9zpIyt2-1668779373753)(C:\Users\YiYouLi\AppData\Roaming\Typora\typora-user-images\1668778098319.png)]

集合1 & 集合2——》判断交集:两个集合里面共同有的数据
集合1 | 集合2——》判断并集:两个集合里面全部数据(重复的数据只算一份)
集合1 - 集合2——》判断差集:减去共有的数据,剩下的数据
in / not in
set1={1,2,3}
set2={3,5,6}
print(set1&set2)

{3}

Process finished with exit code 0
set1={1,2,3}
set2={3,5,6}
print(set1|set2)

{1, 2, 3, 5, 6}

Process finished with exit code 0
set1={1,2,3}
set2={3,5,6}
print(set1-set2)

{1, 2}

Process finished with exit code 0
set1={1,2,3}
set2={3,5,6}
print(2 in set1)
print(5 in set1)

True
False

Process finished with exit code 0

2.1.2 集合的增删改查(集合只有增和删)

2.1.2.1 add()

参数作为要添加的对象,通过多次添加数据可以发现添加后的元素位置就不确定了

s={"BM"}
s.add("baoshijie")
print(s)

{'baoshijie', 'BM'}

Process finished with exit code 0
2.1.2.2 update()

参数为序列类型,会将每一个元素拆分后随机添加到序列中

s={"BM"}
s.update("baoshijie")
print(s)

{'i', 'e', 'b', 'BM', 'j', 'h', 's', 'a', 'o'}

Process finished with exit code 0

2.1.2.3 pop()

随机删除一个元素

s={"BM"}
s.update("baoshijie")
print(s)
s.pop()
print(s)

{'b', 'a', 'e', 'o', 's', 'BM', 'j', 'i', 'h'}
{'a', 'e', 'o', 's', 'BM', 'j', 'i', 'h'}

Process finished with exit code 0
2.1.2.4 remove()

删除指定元素,原集合中没有就会报错

s={"BM"}
s.update("baoshijie")
s.remove("o")
print(s)

{'BM', 'i', 'a', 's', 'j', 'e', 'b', 'h'}

Process finished with exit code 0
2.1.2.5 discard()

删除指定元素,原集合中没有但不会报错

s={"BM"}
s.update("baoshijie")
s.discard("5")
print(s)

{'s', 'j', 'b', 'o', 'BM', 'h', 'a', 'e', 'i'}

Process finished with exit code 0
2.1.2.6 del()

删除集合,但会报错

s={"BM"}
del s
print(s)

Traceback (most recent call last):
  File "E:\InPrg\ProPrg\Program Folder\code\test_python\1118.py", line 165, in <module>
    print(s)
NameError: name 's' is not defined

Process finished with exit code 1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值