Python学习day7(字典和集合)

字典

应用场合

用来存储一堆键值对形式的数据。

语法

符号:大括号,数据为键值对形式出现,各个键值对之间用逗号隔开。

#有数据的字典
dict1 = {'name':'tom','age':18,"gender":'男'}
#空字典1
dict2 = {}
#空字典2
dict3 = dict()
print(dict2==dict3)//True

字典的常用操作

增加&修改

字典序列[key] = 值
如果key不存在则新增此键值对。
如果key存在则修改这个key对应的值。

dict1 = {'name':'tom','age':18,"gender":'男'}
dict1['id'] = 67 #增加
dict1['name'] = "rose" #修改

注意:字典是可变类型。

删除

如果要删除的字典或字典里的键不存在,则报错

  • del()/del:删除字典或字典中指定的键值对
dict1 = {'name':'tom','age':18,"gender":'男'}
#del()
del(dict1['name'])
print(dict1)#{'age': 18, 'gender': '男'}
#del 
del dict1['age']
print(dict1)#{'gender': '男'}
dict1 = {'name':'tom','age':18,"gender":'男'}
# del()
del(dict1)
print(dict1)#NameError: name 'dict1' is not defined
dict1 = {'name':'tom','age':18,"gender":'男'}
# del
del dict1
print(dict1)#NameError: name 'dict1' is not defined
  • clear():清空字典里全部数据
dict1 = {'name':'tom','age':18,"gender":'男'}
dict1.clear()
print(dict1) #{}

查找

  • 通过key查找
    如果当前查找的key存在,则返回对应的值,否则报错
dict1 = {'name':'tom','age':18,"gender":'男'}
print(dict1['name'])#tom
print(dict1['id'])#KeyError: 'id'
  • get():语法为:字典序列.get(key,默认值),如果当前查找的key不存在则返回默认值(第二个参数),如果省略了第二个参数,则返回None
dict1 = {'name':'tom','age':18,"gender":'男'}
print(dict1.get('name'))#tom
print(dict1.get('id',"default00"))#default00
print(dict1.get("id"))#None
  • keys():以’dict_keys’类型返回该字典所有的key。
dict1 = {'name':'tom','age':18,"gender":'男'}
print(dict1.keys())#dict_keys(['name', 'age', 'gender'])
  • values:以’dict_values’类型返回该字典所有的value。
dict1 = {'name':'tom','age':18,"gender":'男'}
print(dict1.values())#dict_values(['tom', 18, '男'])
  • items:以’dict_items’类型返回该字典所有的key和value
dict1 = {'name':'tom','age':18,"gender":'男'}
print(dict1.items())#dict_items([('name', 'tom'), ('age', 18), ('gender', '男')])

字典的循环遍历

用keys()遍历字典所有的key

dict1 = {'name':'tom','age':18,"gender":'男'}
for key in dict1.keys():
    print(key)

用values()遍历字典所有的value

dict1 = {'name':'tom','age':18,"gender":'男'}
for v in dict1.values():
    print(v)

用 items()遍历字典中所有元素

dict1 = {'name':'tom','age':18,"gender":'男'}
for item in dict1.items():
    print(item)

在这里插入图片描述
用 items同时遍历key和value

dict1 = {'name':'tom','age':18,"gender":'男'}
for key,value in dict1.items():
    print(f'{key}--{value}')

在这里插入图片描述

集合

应用场景

集合中的元素是不允许重复的,如果需要存储一堆不重复的数据,可以使用集合。
【特点】:集合中的数据是无序的,不可重复的。

s1 = {10,20,30,40,50,50}
#集合自动去重
print(s1)#{40, 10, 50, 20, 30}

创建集合

创建集合可以使用{ }或 set(),但是如果要创建空集合只能使用set(),因为{ }已经被用来创建空字典。

# 1. 创建有数据的集合
s1 = {10, 20, 30, 40, 50}
print(s1)#{40, 10, 50, 20, 30}

s2 = {10, 30, 20, 40, 30, 20}
print(s2)#{40, 10, 20, 30}

s3 = set('abcdefg')
print(s3)#{'f', 'g', 'e', 'a', 'd', 'b', 'c'}

s4 = set([10,20,30])
print(s4)#{10, 20, 30}

# 2. 创建空集合: set()
s4 = set()
print(s4)#set()

【注意】:利用set创建集合,set()中的参数只能有一个,而且还是可迭代序列

s3 = set("897","fehi")
print(s3)#TypeError: set expected at most 1 arguments, got 2
s3 = set(1234)
print(s3)#TypeError: 'int' object is not iterable

增加数据

  • add():添加一个数据
s1 = {10,20}
s1.add(30)
s1.add(10)
print(s1)#{10, 20, 30}
  • update():添加一组数据序列,不可添加单个数据,添加的只能是一个可迭代的数据对象。
s1 = {10,20}
s1.update(123)
print(s1)#TypeError: 'int' object is not iterable
s1 = {10,20}
s1.update("123")
print(s1)#{10, '3', 20, '2', '1'}
s1.update([100,200])
print(s1)#{'3', 100, 200, 10, 20, '1', '2'}

删除数据

  • remove():删除集合中指定数据,如果数据不存在则报错
s1 = {10, 20, 30, 40, 50}

# remove(): 删除指定数据,如果数据不存在报错
s1.remove(10)
print(s1)

s1.remove(10)  # 报错
print(s1)
  • discard():删除集合中指定数据,如果数据不存在不会报错
s1 = {10, 20, 30, 40, 50}

# discard():删除指定数据,如果数据不存在不报错
s1.discard(10)
print(s1)

s1.discard(10)
print(s1)
  • pop():随机删除集合中某个元素,并返回这个数据

【注意】:并非真正的随机删除,而是因为集合中的数据是乱序的(但仍以一定规则排列),我们如果不深入研究集合底层数据元素的排序规则,也就不会知道究竟会删除哪一个元素。
例如:

s1 = {10,20,30,40,50}
print(s1)#{40, 10, 50, 20, 30}
a = s1.pop()
print(a)#40

不论运行多少次,每次都删除的是40,可见并不是随机的,只是40刚好排在了第一个,所以它每次删除的都是栈顶元素(压栈)。

判断数据是否在集合中

  • in:判断数据在集合序列中
  • not in:判断数据不在集合序列中
s1 = {10, 20, 30, 40, 50}
print(10 in s1)#True
print(10 not in s1)#False
发布了68 篇原创文章 · 获赞 57 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43598138/article/details/104964460