python教程_小白入门15/2020/8/26
学习目标
p102 作业讲解1
# 作业讲解
# 1.声明一个字典保存学生的信息,学生信息中包括:姓名,年龄,成绩,电话,性别(男,女,不明)
# student = {'name': 'zhangsan', 'age': 18, 'score': 98, 'tel': '13865465464', 'gender': 'female'}
# 2.声明一个列表,在列表中保存6个学生的信息()
students = [
{'name': 'zhangsan', 'age': 18, 'score': 98, 'tel': '13865465464', 'gender': 'female'},
{'name': 'lisi', 'age': 22, 'score': 95, 'tel': '17844477854', 'gender': 'male'},
{'name': 'tom', 'age': 19, 'score': 98, 'tel': '13146246423', 'gender': 'unknow'},
{'name': 'jack ', 'age': 20, 'score': 58, 'tel': '19185466845', 'gender': 'male'},
{'name': 'tony', 'age': 15, 'score': 52, 'tel': '18154447813', 'gender': 'female'},
{'name': 'zhangliang', 'age': 17, 'score': 89, 'tel': '15698665552', 'gender': 'unknow'}
]
# # 统计不及格学生的个数-----------------------------------------------------------
# count1 = 0
# for student in students:
# if student['score'] < 60:
# count1 += 1
# print('不及格的学生的个数有%d个' % count1)
# # 不及格的学生的个数有2个
# # 打印不及格学生的名字和对应的成绩------------------------------------------------
# for student in students:
# if student['score'] < 60:
# print('不及格的学生是%s,成绩是%d' % (student['name'], student['score']))
# # 用d表示整数,s表示字符串,多个参数时尽量使用括号
# # 不及格的学生是jack, 成绩是58
# # 不及格的学生是tony, 成绩是52
# # 统计未成年学生的个数---------------------------------------------------------
# count2 = 0
# for student in students:
# if student['age'] < 18:
# count2 += 1
# print('未成年的学生的个数为%d' % count2)
# # 未成年的学生的个数为2
# # 打印手机尾号是4的学生的名字----------------------------------------------------
# for student in students:
# if student['tel'].endswith('4'):
# print('手机尾号为4的学生的名字为%s ' % student['name'])
# # 如果在数据库里 tel的数据类型是int,需要%10,就能得到最后一位的数字
# # 手机尾号为4的学生的名字为zhangsan
# # 手机尾号为4的学生的名字为lisi
# # 打印最高分和对应的学生的名字---------------------------------------------------
# max_score = students[0]['score'] # 假设第一个学生的成绩就是最高分
# max_index = 0
# # 带下标的遍历使用enumerate方法,不记得在哪里学过
#
# for i, student in enumerate(students):
# if student['score'] > max_score:
# max_score = student['score']
# max_index = i
# print('最高成绩是%d' % max_score) # 最高成绩是98
# for student in students:
# if student['score'] == max_score:
# print('最高分的学生姓名是:%s' % student['name'])
# # 最高分的学生姓名是:zhangsan
# # 最高分的学生姓名是:tom
# # 删除性别不明的所有学生---------------------------------------------------------
# # 方法1
# new_student = []
# for student in students:
# if student['gender'] != 'unknow':
# new_student.append(student)
# # print('性别不明的学生姓名为:%s' % student['name'])
# print(new_student)
# # 方法2,切片是浅复制,感觉这个方法更精妙
# for student in students[::]:
# if student['gender'] == 'unknow':
# students.remove(student)
# print(students)
# # [{'name': 'zhangsan', 'age': 18, 'score': 98, 'tel': '13865465464', 'gender': 'female'}, {'name': 'lisi', 'age': 22, 'score': 95, 'tel': '17844477854', 'gender': 'male'}, {'name': 'jack ', 'age': 20, 'score': 58, 'tel': '19185466845', 'gender': 'male'}, {'name': 'tony', 'age': 15, 'score': 52, 'tel': '18154447813', 'gender': 'female'}]
# # [{'name': 'zhangsan', 'age': 18, 'score': 98, 'tel': '13865465464', 'gender': 'female'}, {'name': 'lisi', 'age': 22, 'score': 95, 'tel': '17844477854', 'gender': 'male'}, {'name': 'jack ', 'age': 20, 'score': 58, 'tel': '19185466845', 'gender': 'male'}, {'name': 'tony', 'age': 15, 'score': 52, 'tel': '18154447813', 'gender': 'female'}]
# 将列表按照学生成绩从小到大排列--冒泡排序的思想-------------------------------------
for j in range(len(students) -1):
for i in range(len(students) -1 -j): # 注意一定要有 -j
if students[i]['score'] > students[i+1]['score']:
students[i],students[i+1]=students[i+1],students[i]
print(students)
# [{'name': 'tony', 'age': 15, 'score': 52, 'tel': '18154447813', 'gender': 'female'}, {'name': 'jack ', 'age': 20, 'score': 58, 'tel': '19185466845', 'gender': 'male'}, {'name': 'zhangliang', 'age': 17, 'score': 89, 'tel': '15698665552', 'gender': 'unknow'}, {'name': 'lisi', 'age': 22, 'score': 95, 'tel': '17844477854', 'gender': 'male'}, {'name': 'zhangsan', 'age': 18, 'score': 98, 'tel': '13865465464', 'gender': 'female'}, {'name': 'tom', 'age': 19, 'score': 98, 'tel': '13146246423', 'gender': 'unknow'}]
P103 作业讲解2
# 用三个元组表示三门学科的选课学生姓名(一个学生可以同时选择多门课程)
sing = ('李白','杜甫','白居易','孟浩然','王昌龄','李商隐','骆宾王','亚里士多德')
dance = ('王安石','孟浩然','白居易','王昌龄','托尔斯泰','马克思','苏轼','曹操')
rap = ('李清照','托尔斯泰 ','亚里士多德','李商隐','王昌龄','李白')
# 元组之间支持加法
# 1.求选课学生总共有多少人------------------------------------------------
total = sing+dance+rap
print(total)# ('李白', '杜甫', '白居易', '孟浩然', '王昌龄', '李商隐', '骆宾王', '亚里士多德', '王安石', '孟浩然', '白居易', '王昌龄', '托尔斯泰', '马克思', '苏轼', '曹操', '李清照', '托尔斯泰 ', '亚里士多德', '李商隐', '王昌龄', '李白')
# 使用集合set可以去重
x = set(total)# {'李白', '曹操', '李清照', '王安石', '白居易', '李商隐', '杜甫', '骆宾王', '孟浩然', '托尔斯泰', '马克思', '王昌龄', '托尔斯泰 ', '亚里士多德', '苏轼'}
print(x)
print(len(x))#15
# 2.求只选了第一个学科的人的数量和对应的名字--------------------------------------------------------
sing_only = []
for p in sing:
if p not in dance and p not in rap:
sing_only.append(p)
print('只选择了第一个学科的有{}人,分别是{}:'.format(len(sing_only),sing_only))
# 只选择了第一个学科的有2人,分别是['杜甫', '骆宾王']:
# 3.求只选了一门学科的学生的数量和对应的名字--------------------------------------------------------
# 4.求只选了两门科学的学生的数量和对应的名字
# 5.求选了三门学科的学生的数量和对应的名字
p_dict = {}
for name in total:
if name not in p_dict:
p_dict[name] = total.count(name)
print(p_dict)
# {'李白': 2, '杜甫': 1, '白居易': 2, '孟浩然': 2, '王昌龄': 3, '李商隐': 2, '骆宾王': 1, '亚里士多德': 2, '王安石': 1, '托尔斯泰': 1, '马克思': 1, '苏轼': 1, '曹操': 1, '李清照': 1, '托尔斯泰 ': 1}
k1 =[]
k2 =[]
k3= []
for k,v in p_dict.items():
if v == 1:
k1.append(k)
elif v == 2:
k2.append(k)
elif v ==3:
k3.append(k)
print('只选择了一门课程的是{}'.format(k1))
print('只选择了两门课程的是{}'.format(k2))
print('选择了三门课程的是{}'.format(k3))
# 只选择了一门课程的是['杜甫', '骆宾王', '王安石', '托尔斯泰', '马克思', '苏轼', '曹操', '李清照', '托尔斯泰 ']
# 只选择了两门课程的是['李白', '白居易', '孟浩然', '李商隐', '亚里士多德']
# 选择了三门课程的是['王昌龄']
P104 集合的基本使用
# 集合是一个不重复的无序的,可以使用{}或者set来表示
# {}有两种意思,字典和集合都可以使用大括号
# 如果放的是键值对就是字典,如果放的是单个的值就是集合
person = {'name': 'zhangsan', 'age': 44, 'addr': '山东济南'}
x = {'hello', 45, 22.3, 'weight'}
print(type(person)) # <class 'dict'>
print(type(x)) # <class 'set'>
# 集合可以自动去重,并且是无序的
names = {'张三', '李四', '王五', '赵六', '张三'}
print(names) # {'李四', '张三', '赵六', '王五'}
# set能不能进行增删改查
names.add('秦始皇')
print(names) # {'赵六', '张三', '王五', '李四', '秦始皇'}
# names.clear()
# print(names) # set():空集合的表示方式是set(),空字典的表示方式是{}
# pop()方法是从中随机删除一个,删除的是哪一个我们不知道
names.pop()
print(names) # {'赵六', '王五', '李四', '秦始皇'},随机删除掉了张三
# 如果原本就是一个空的集合,再进行删除就会报错,报错的时候code是1,正常运行code是0
# union()方法是将两个集合拼接到一起,生成一个新的集合,也就是求并集
print(names.union({'钢铁侠', '闪电侠'})) # {'钢铁侠', '赵六', '闪电侠', '秦始皇', '王五', '张三'}
# 而uodate方法,A.update(B),将B拼接到A里,放集合放元组都可以的
print(names.update(['孙悟空', '雷恩加尔'])) # None
print(names) # {'张三', '秦始皇', '雷恩加尔', '王五', '孙悟空', '赵六'}
print('jack' in names)# 用来判断集合中是否存在
# False
P105 集合的运算符的使用
sing = {'李白','杜甫','白居易','孟浩然','王昌龄','李商隐','骆宾王','亚里士多德'}
dance = {'王安石','孟浩然','白居易','王昌龄','托尔斯泰','马克思','苏轼','曹操'}
rap = {'李清照','托尔斯泰 ','亚里士多德','李商隐','王昌龄','李白'}
# total = sing+dance+rap # 报错,set不支持加法操作
# 集合的优点,能够支持很多算术运算符
print(sing - dance) # 求A和B的差集,也就是A独有的元素
# {'骆宾王', '李白', '李商隐', '杜甫', '亚里士多德'}
print(sing & rap) # 求A和B的交集,A 和B共同的部分 A n B
# {'亚里士多德', '王昌龄', '李白', '李商隐'}
print(sing | rap) # A和B的并集 A U B
# {'杜甫', '白居易', '王昌龄', '亚里士多德', '骆宾王', '孟浩然', '托尔斯泰 ', '李白', '李清照', '李商隐'}
print(sing ^ rap) # A和B的并集 减去 A和B的交集
# {'托尔斯泰 ', '杜甫', '白居易', '李清照', '孟浩然', '骆宾王'}
P 106 eval和json的使用
# # 集合的练习
# # 去重,排序
# nums = [5, 8, 7, 6, 1, 3, 5, 1, 8, 4]
# # 去重
# x = set(nums)
# y = list(x)
# y.sort(reverse=False) # false就是正序,True就是倒叙
# print(y) # [1, 3, 4, 5, 6, 7, 8]
# ---------------------------------
# 内置类 list tuple set
nums = [9, 8, 4, 3, 2, 1]
x = tuple(nums)
print(type(x)) # <class 'tuple'>
y = set(x)
print(type(y)) # <class 'set'>
z = list({'name': 'zhangsan', 'age': 16, 'addr': 'zhongdu'})
print(z) # # ['name', 'age', 'addr'] 字典在强制转换为列表时只剩下key
# python里有一个比较强大的内置函数,可以执行字符串中的代码,eval
a = 'input("请输入您的用户名")' # a是一个字符串
print(a)
# eval(a) # 请输入您的用户名,直接把a里面的内容执行了
b = '1+5'
print(eval(b))
# json的使用,把列表,元组,字典转换成为json字符串
# json是一种轻量级的数据交换格式,采用完全独立于编程语言的文本格式来存储和表示数据,json本质是一个字符串
# 举例: '{"name":"zhangsan","age":18,"addr":"jinan"}'
# json是长的非常像 字典的 一个字符串,json中用的是双引号,最最外还有一个单引号。
import json
person = {'name': 'zhangsan', 'age': 18, 'addr': 'shandong'}
m = json.dumps(person) # dumps将字典、列表、集合、元组等转换成为json字符串
print(m) # {"name": "zhangsan", "age": 18, "addr": "shandong"}
print(type(m)) # <class 'str'>
# print(m['name']) # 报错,m是字符串,已经不是字典了
n = '{"name":"lisi","age":18,"gender":"female"}'
# p = eval(n)
# print(type(p)) # <class 'dict'>
s = json.loads(n) # loads可以将json字符串转u换为python里的数据
print(s)
print(type(s)) # print(type(s))
# 将json字符串转换为python里的数据 常用的还是loads,而不是eval
# python JSON
# True true
# False false
# 字符串 字符串
# 字典 对象
# 列表,元组 数组
print(json.dumps(['hello', 'good', 'yes', True]))
print(json.dumps(('hello', 'good', 'yes', False)))
# ["hello", "good", "yes", true]
# ["hello", "good", "yes", false]