python基础笔记——字典与集合

一、字典

1、字典的特点

  • 符号为大括号
  • 数据为键值对形式出现
  • 各个键值对之间用逗号隔开
dict1 = {'name': 'Tom', 'age': 20, 'gender': '男'}
# 空字典
dict2 = {}
dict3 = dict()

2、字典的常见操作

2.1 增

写法:字典序列[key] = 值
注意:如果key存在则修改这个key对应的值,如果不存在则新增此键值对

dict1 = {'name': 'Tom', 'age': 22, 'gender': '男'}

dict1['name'] = 'Lisa'
print(dict1)  # {'name': 'Lisa', 'age': 22, 'gender': '男'}

dict1['id'] = 1
print(dict1)  # {'name': 'Lisa', 'age': 22, 'gender': '男', 'id': 1}
2.2 删
  • del()/del:删除字典或删除字典中指定的键值对
  • clear():清空字典
dict1 = {'name': 'Tom', 'age': 22, 'gender': '男'}

# del 删除字典或键值对
# del(dict1)
# print(dict1)  # NameError: name 'dict1' is not defined

del dict1['name']
print(dict1)  # {'age': 22, 'gender': '男'}

# clear()
dict1.clear()
print(dict1)  # {}
2.3 改

写法:字典序列[key] = 值
注意:如果key存在则修改这个key对应的值,否则新增

2.4 查

1.写法:key值查找
如果当前查找的key存在,则返回对应的值,否则报错

dict1 = {'name': 'Tom', 'age': 22, 'gender': '男'}

# key值查找
print(dict1['name'])  # Tom
print(dict1['id'])  # KeyError: 'id'

2. 写法:函数

  • get()
    语法:
字典序列.get(key, 默认值)

注意:如果当查找的key不存在则返回第二个参数(默认值),如果省略第二个参数,则返回None

dict1 = {'name': 'Tom', 'age': 22, 'gender': '男'}

print(dict1.get('name'))  # Tom
print(dict1.get('id'))  # None
print(dict1.get('id', 404))  # 404
  • keys()
# 2.2 keys() 查找字典中所有的key,返回可迭代的对象
print(dict1.keys())  # dict_keys(['name', 'age', 'gender'])

values()

# 2.3 values() 查找字典中所有的value,返回可迭代的对象
print(dict1.values())  # dict_values(['Tom', 22, '男'])
  • items()
# 2.4 items() 查找字典中所有的键值对,返回可迭代的对象,里面数据是元组,元组数据1是key,数据2是value
print(dict1.items())  # dict_items([('name', 'Tom'), ('age', 22), ('gender', '男')])

3、字典的循环遍历

3.1 遍历字典的key
dict1 = {'name': 'Tom', 'age': 22, 'gender': '男'}

for key in dict1.keys():
    print(key)
3.2 遍历字典的value
dict1 = {'name': 'Tom', 'age': 22, 'gender': '男'}

for value in dict1.values():
    print(value)
3.3 遍历字典的元素
dict1 = {'name': 'Tom', 'age': 22, 'gender': '男'}

for item in dict1.items():
    print(item)
dict1 = {'name': 'Tom', 'age': 22, 'gender': '男'}

for key, value in dict1.items():
    print(f'{key} = {value}')
# name = Tom
# age = 22
# gender = 男

二、集合

特点:

  • 集合数据会自动去重
  • 集合是无序的

1、创建集合

创建集合使用{}或者set(),但是如果要创建空集合只能使用set(),因为{}用来创建空字典

# 特点一:集合是无序的
s1 = {11, 22, 33, 44, 55}
print(s1)  # {33, 11, 44, 22, 55}

# 特点二:集合会自动去重
s2 = {11, 22, 22, 31, 12, 34}
print(s2)  # {34, 11, 12, 22, 31}

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


# 2.创建空集合
s4 = set()
print(s4)  # set()
print(type(s4))  # <class 'set'>

s5 = {}
print(type(s5))  # <class 'dict'>

2、集合增加数据

  • add()
  • update():追加的数据是序列
s1 = {10, 20}

# add()
s1.add(100)
print(s1)  # {100, 10, 20}

s1.add(10)
print(s1)  # {100, 10, 20}

s2 = {11, 22}

# update() 增加的数据是序列
s2.update([33, 44, 55, 66])
print(s2)  # {33, 66, 11, 44, 22, 55}

3、集合删除数据

  • remove():删除集合中指定数据,如果数据不存在则报错
  • discard():删除集合中指定数据,如果数据不存在也不会报错
  • pop():随机删除某个数据,并返回这个数据
s1 = {11, 22, 33, 44, 55}

# remove():不存在报错
s1.remove(22)
print(s1)  # {33, 11, 44, 55}

# s1.remove(22)
# print(s1)  # KeyError: 22


# discard():不存在不报错
s1.discard(55)
print(s1)  # {33, 11, 44}
s1.discard(55)
print(s1)  # {33, 11, 44}


# pop()
del_num = s1.pop()
print(del_num)  # 33
print(s1)  # {11, 44}

4、集合查找数据

  • in:判断数据在集合序列
  • not in:判断数据不在集合序列
s1 = {11, 22, 33, 44, 55}

print(11 in s1)  # True
print(11 not in s1)  # False
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值