元组和字典(2021-9-23)
1、什么是元组(tuple)
元组是容器型数据类型(序列),将()作为容器的标志里面多个元素用逗号隔开:(元素1, 元素2, 元素3…)
元组不可变(不支持增删改);元组是有序的(支持下标操作)
元素:任何类型的数据
T0 = (10, 20, 30) # (10, 20, 30) <class 'tuple'>
print(T0, type(T0))
T1 = (10, 'ABC', False, [10, 20]) # (10, 'ABC', False, [10, 20])
print(T1)
T2 = () # 空元组
print(T2, type(T2)) # () <class 'tuple'>
2、元组就是不可变的列表
列表中除了和增、删、改相关操作以外的操作元组都支持
例如:查、相关操作、相关方法(除了增删改相关的)
T = (10, 20, 30, 40, 50)
print(T[-1])
print('-----------------------------------------------------------')
print(T[1])
print('-----------------------------------------------------------')
num =23
t_num = type(num)
if t_num == int or t_num == float or t_num ==str:
print('是数字或者字符串')
if t_num in (int, float, str):
print('是数字或者字符串')
T = (10, 20, 30, 40, 50)
print(T.count(10))
print(T.index(20))
print(max(T), min(T))
print(sum(T))
print(sorted(T))
print(tuple('abc look'))
3、元组特殊或者常用的操作
1)只有一个元素的元组:(元素,)
list1 = [10]
print(list1, type(list1)) # [10] <class 'list'>
t0 = (10)
print(t0, type(t0)) # 10 <class 'int'>
t1 = (10, )
print(t1, type(t1)) # (10,) <class 'tuple'>
2)元组在没有歧义的情况下可以省略()。
直接用逗号将多个数据隔开,表示的也是一个元组
t2 = (10, 20, 30, 40)
print(t2, type(t2)) # (10, 20, 30, 40) <class 'tuple'>
t2 = 10, 20, 30, 40
print(t2, type(t2)) # (10, 20, 30, 40) <class 'tuple'>
3)同时使用多个变量获取元组的元素
t3 = ('小明', 18, 90)
x = t3[0]
print(x) # 小明
a、让变量的个数和元组中元素个数保持一致
name, age, scoe = t3
print(name, age, scoe) # 小明 18 90
point = (100, 200)
x, y = point
print(x, y) # 100 200
b、让变量的个数小于元组元素的个数,但是必须在某一个变量前加*
先让不带的变量按照位置获取元素,剩下的部分全部保存到带的变量对应的列表中
t4 = ('
小明', 18, 4, 46, 57, 35, 65, 76, 35)
x, y =t4 # 报错(变量跟元组中元素数量不一致)
x, y, *z = t4
print(x, y, z) # 小明 18 [4, 46, 57, 35, 65, 76, 35]
x, *y, z = t4
print(x, y, z) # 小明 [18, 4, 46, 57, 35, 65, 76] 35
*x, y, z = t4
print(x, y, z) # ['小明', 18, 4, 46, 57, 35, 65] 76 35
4、字典
students = {
'name':'张三',
'contacts':'张四',
'age':20,
'tel':'11111111111',
'score':28
}
print(students['name']) # 张三
1、什么是字典(dict)
1)字典是容器型数据类型(序列),将{}作为容器的标志,里面多个键值对用逗号隔开{键1:, 键2:, 键3:, …}
2)字典是可变的(支持增删改);字典是无序的(不支持下标操作)
3)元素 - 键值对
键 - 必须是不可变的数据,例如:元组、数字、字符串;列表和字典不行(可变)
值(才是真正想要保存的数据) - 没有要求
print({'a':2, 'c':20} == {'c': 20, 'a':2}) # True
1)空字典
dict1 ={}
print(dict1, type(dict1)) # {} <class 'dict'>
2)键是不可变的数据
dict2 = {'a':10, 1:20, (1, 2): 30}
print(dict2) # {'a': 10, 1: 20, (1, 2): 30}
dict3 = {'a':10, 1:20, [1, 2]: 30}
print(dict3) # 报错(字典里面有列表,列表是可变的)
3)键是唯一的
dict3 = {'a':10, 1:20, 'a': 30}
print(dict3) # {'a': 30, 1: 20}
5、字典的查操作是获取字典的值
1、查单个
1)字典[键] - 获取字典中指定键对应的值;当键不存在的时候会报错
2)字典.get(键) == 字典.get(键, None) - 获取字典中指定键对应的值
3)字典.get(键, 默认值) - 只有在键不存在的情况下才会反馈默认值,默认值可以直接填写
dog = {'name': '小邦浪', 'breed': '哈士奇', 'age': 1}
print(dog['name']) # 小邦浪
print({'name': '小邦浪', 'breed': '哈士奇', 'age': 1}) # {'name': '小邦浪', 'breed': '哈士奇', 'age': 1}
print(dog.get('name')) # 小邦浪
print(dog['gender']) # 报错
print(dog.get('gender')) # None
dog = {'name': '邦浪'}
print(dog.get('age', 3)) # 3
2、遍历
for 变量 in 字典:
循环体
变量取到的是键
print('----------------------------------------------------------------------------')
dog = {'name': '小邦浪', 'breed': '哈士奇', 'age': 1, 'color':'黑色'}
for a in dog:
print(a, dog[a])
用一个字典保存一个班级信息
class1 = {
'name': 'Python2106',
'address': '9教',
'lecturer': {
'name': '余婷',
'age': 18,
'tel': '13678192302',
'QQ': '726550822'
},
'head_teacher': {
'name': '张瑞燕',
'tel': '110',
'QQ': '7283211'
},
'students': [
{
'name': '陈来',
'age': 20,
'gender': '女',
'score': 98,
'contacts': {
'name': 'p2',
'tel': '120'
}
},
{
'name': '葛奕磊',
'age': 25,
'gender': '男',
'score': 80,
'contacts': {
'name': 'p1',
'tel': '119'
}
}
]
}
- 获取班级的名字
print(class1['name'])
- 获取讲师的名字和年龄
print(class1.get('lecturer').get('name'), class1.get('lecturer').get('age'))
- 获取班主任的名字和电话
print(class1.get('head_teacher').get('name'), class1.get('head_teacher').get('tel'))
- 获取第一个学生的姓名
print(class1.get('students')[0].get('name'))
- 获取所有学生的联系人的名字
for a in class1['students']:
print(a['contacts']['name'])
- 计算所有学生的平均分
1. sum1 = 0
all_student = class1['students']
for stu in all_student:
print(stu['score'])
sum1 += stu['score']
print(sum1/2)
6、字典的增删改
1、增 - 添加键值对
2、改 - 修改键对应的值
语法:字典[键] = 值 - 当键存在的时候是修改,键不存在就是添加
goods = {
'name':'泡面',
'price':3.5
}
print(goods) # {'name': '泡面', 'price': 3.5}
goods['count'] = 10
print(goods) # {'name': '泡面', 'price': 3.5, 'count': 10}
3、删
1)del 字典[键] - 删除字典中指定键对应的键值对
goods = {'name':'泡面', 'price':'3.5', 'count':'10'}
del goods['price']
print(goods) # {'name': '泡面', 'count': '10'}
2)字典.pop(键) - 取出字典中指定键对应值
goods = {'name':'泡面', 'price':'3.5', 'count':'10'}
result = goods.pop('price')
print(goods) # {'name': '泡面', 'count': '10'}
print(result) # 3.5