1.字典不支持:+、*、<、>、<=、>=
2.字典支持:==、!=
3.in 和 not in
键 in 字典 - 判断字典中是否存在指定的键
d1 = {'a': 10, 'b': 20}, 'c': 30}
print(30 in d1) # False
print('b' in d1) # True
4.dict()
dict(数据) - 将数据转换成字典
数据的要求
- 数据本身是一个序列
- 序列中的元素必须是长度为2的小序列
- 小序列的第一个元素必须是不可变的
result = dict([(1, 2), 'ab', ['b': 100]])
print(result) # {1: 2, 'a': 'b', 'b': 100}
注意:将字典转换成列表或者元组的时候,将字典中的键作为列表、元组中的元素
d1 = {'a': 10, 'b': 20}, 'c': 30}
print(list(d1)) # ['a', 'b', 'c']
print(tuple(d1)) # ('a', 'b', 'c')
5.相关方法
1)字典.lear() - 清空字典
d1 = {'a': 10, 'b': 20}, 'c': 30}
d1.clear()
print(d1) # {}
2)字典.copy()
stu1 = {'name': '小明', 'gender': '男', 'age': 18}
print(stu1)
stu2 = stu1.copy()
print(stu2)
stu2['name'] = '张三'
print(stu2)
3)字典.items() - 将字典中的元素转换成元组,返回一个新的序列
{键1: 值1, 键2: 值2} ->[(键1: 值1), (键2: 值2)]
stu1 = {'name': '小明', 'gender': '男', 'age': 18}
result = stu1.items()
print(result) # dict_items([('name', '小明'), ('gender', '男'), ('age', 18)])
print(list(result)) # [('name', '小明'), ('gender', '男'), ('age', 18)]
字典推导式:{键值对 for 变量 in 序列}、{键值对 for 变量 in 序列 if 条件语句}
练习:使用列表推导式,将一个字典的值全部乘以10
d2 = {'a': 2, 'b': 34, 'c': 21}
d3 = {x: d2[x]*10 for x in d2}
print(d3) # {'a': 20, 'b': 340, 'c': 210}
d3 = {key: value*10 for key, value in d2.items()}
print(d3) # {'a': 20, 'b': 340, 'c': 210}
d3 = dict([(x, y*10) for x, y in d2.items()])
print(d3) # {'a': 20, 'b': 340, 'c': 210}
练习:使用推导式交换字典的键和值
d2 = {'a': 2, 'b': 34, 'c': 21}
d3 = {value: key for key, value in d2.items()}
print(d3) # {2: 'a', 34: 'b', 21: 'c'}
4)字典.keys() - 获取字典所有的键,返回一个新的序列
d2 = {'a': 2, 'b': 34, 'c': 21}
print(d2.keys()) # dict_keys(['a', 'b', 'c'])
print(list(d2.keys())) # ['a', 'b', 'c']
5)字典.values() - 获取字典所有的值,返回一个新的序列
d2 = {'a': 2, 'b': 34, 'c': 21}
print(d2.values()) # dict_values([2, 34, 21])
print(list(d2.values())) # [2, 34, 21]
6)字典.setdefault(键, 值)
d2 = {'a': 2, 'b': 34, 'c': 21}
d2.setdefault('d', 30)
print(d2) # {'a': 2, 'b': 34, 'c': 21, 'd': 30}
d1 = {'a': 10}
d1.setdefault('a', 100)
print(d1) # {'a': 10}
goods_list = [
{'name': '泡面', 'price': 4, 'discount': 0.9, 'count': 100},
{'name': '火腿肠', 'price': 2, 'count': 120},
{'name': '矿泉水', 'price': 1, 'count': 500},
{'name': '面包', 'price': 5, 'count': 120, 'discount': 0.75}
]
for goods in goods_list:
goods.setdefault('discount', 1)
print(goods_list)
7)字典.update(序列) - 将序列中的元素全部添加到字典中
序列 - 是字典或者是能转换成字典的序列
d1 = {'a': 10, 'b': 20}
d2 = {'name': '小明', 'age': 18, 'a': 200}
d1.update(d2)
print(d1) # {'a': 200, 'b': 20, 'name': '小明', 'age': 18}
d1.update(['mn', (1, 2)])
print(d1) # {'a': 200, 'b': 20, 'name': '小明', 'age': 18, 'm': 'n', 1: 2}
集合
1.什么是集合
集合是容器型数据类型(序列),将{}作为容器的标志,里面多个元素用逗号隔开:{元素1, 元素2, 元素3, …}
集合是可变的;集合是无序的
元素 - 必须是不可变的数据;唯一
1)空集合
s1 = set()
print(len(s1), type(s1)) # 0 <class 'set'>
2)集合元素必须是不可变的数据
s2 = {1, 'abc', (23, 4)}
print(s2) # {(23, 4), 1, 'abc'}
# s2 = {1, 'abc', [23, 4]} # 报错
3)集合无序
print({1, 2, 3} == {3, 2, 1}) # True
4)元素是唯一的
s3 = {1, 2, 3, 1, 1, 4, 2}
print(s3) # {1, 2, 3, 4}
去重
names = ['张三', '张三', '小明', '李四', '张三', '小明']
print(set(names)) # {'张三', '李四', '小明'}
2.集合的增删查(了解)
1)查 - 只能遍历
hobby = {'玩游戏', '看电影', '打篮球', '爬山', '做饭'}
for i in hobby:
print(i) # 取出来的顺序可能不一样
2)增
- 集合.add(元素)
- 集合.update(序列)
hobby = {'玩游戏', '看电影', '打篮球', '爬山', '做饭'}
hobby.add('游泳')
print(hobby) # {'做饭', '游泳', '看电影', '玩游戏', '打篮球', '爬山'}
hobby.update(('乒乓球', '羽毛球'))
print(hobby) # {'做饭', '乒乓球', '爬山', '羽毛球', '玩游戏', '看电影', '打篮球', '游泳'}
3)删
- 集合.remove(元素)
- 集合.discard(元素) - 如果元素存在不会报错
hobby.remove('做饭')
print(hobby) # {'游泳', '看电影', '羽毛球', '乒乓球', '打篮球', '玩游戏', '爬山'}
hobby.discard('玩游戏')
print(hobby) # {'看电影', '羽毛球', '打篮球', '乒乓球', '游泳', '爬山'}
数学集合运算
python中的集合支持数学集合运算
s1 = {1, 2, 3, 4, 5, 6}
s2 = {4, 5, 6, 7, 8}
1.并集 - |
集合1 | 集合2 - 将两个集合合并成一个集合
result = s1 | s2
print(result) # {1, 2, 3, 4, 5, 6, 7, 8}
2.交集 - &
集合1 & 集合2 - 获取两个集合的公共部分,产生一个集合
result = s1 & s2
print(result) # {4, 5, 6}
3.差集 - -
集合1 - 集合2 - 获取集合1中去掉包含在集合2的元素之后剩下的元素
result = s1 - s2
print(result) # {1, 2, 3}
4.对称差集 - ^
集合1 ^ 集合2 - 将两个集合合并,去掉公共部分,获取剩下的部分
result = s1 ^ s2
print(result) # {1, 2, 3, 7, 8}
5.判断子集关系
-
子集:>=、<=
-
真子集:>、<
集合1 > 集合2 - 判断集合2是否是集合1的真子集
集合1 >= 集合2 - 判断集合2是否是集合1的子集
s3 = {10, 20, 30}
# 真子集:{10, 20}、{20, 30}、{10, 30}、{10}、{20}、{30}、空集
# 子集:{10, 20}、{20, 30}、{10, 30}、{10}、{20}、{30}、空集、{10, 20, 30}
print({100, 200, 300} > {1, 2}) # False
print({100, 200, 300} > {100}) # True