nums =[3,1,9,8,4,7,5,0]# 不调用 直接使用
x = nums[0]# 假设第0个数最大for num in nums:# 遍历整个列表如果列表当中有比第0个数大的数if num > x:# 假设不成立
x = num # 把假设的值改为发现的比第0个数大的数print('发现的最大的数是‰d, 他的下标是‰d'%(x, nums.index(x)))# 不用index方法
i =0
index =0while i <len(nums):if nums[i]> x:
x = nums[i]# 找到后给x
index = i # 记录下来
i +=1print('发现的最大的数是‰d, 他的下标是‰d'%(x, index))
二、删除列表里面的空字符串
words2 =['hello','','good','yes','ok','']
i =0while i <len(words2):if words2[i]=='':
words2.remove(words2[i])
i -=1# 删除一个空字符串 下标也要剪掉
i +=1print(words2)
三、一个学校,有三个办公室,现在有8位老师等待分配工位,请编写程序完成分配
teachers =['A','B','C','D','E','F','G','H']
rooms =[[],[],[]]for i, room inenumerate(rooms):# 找房间print('房间%d里面一共有%d个老师,分别是:'%(i,len(room)), end='')for teacher in teachers:# 找房间里面的人print(teacher, end=' ')print()
四、拿字典里面的次数出现的最大值
chars =['a','c','p','a','s','t','a']# {'a':3, 'c':1, 'p':1, 's':1, 't':1}# 第一种方法:
char_count ={}for char in chars:if char in char_count:print('字典里面有这个字符串%s'% char)
char_count[char]+=1else:
char_count[char]=1# 不在里面就是1print(char_count)# 第二种 拿个数for char in chars:if char notin char_count:
char_count[char]== char_count[char]print(char_count)
五、用三个元组表示三门学科的选课学生姓名(一个学生可以同时选多门课程)
sing =('李白','白居易','李清照','杜甫','小苗','孟浩然')
dance =('韩红','蔡徐坤','李诞','苗苗','李涛','杜甫','小苗')
rap =('小蔡','蔡徐坤','杜甫','小苗','小涛')
one_class =[]for x in sing:if x notin dance and rap:
one_class.append(x)print('求只选了第一个学科的人的数量:{}和对应的名字:{}'.format(len(one_class), one_class))
(3)求只选了一门学科的学生的数量和对应的名字
only_one =[]for i in sing:if(i notin dance and i notin rap):
only_one.append(i)for j in dance:if(i notin sing and i notin rap):
only_one.append(i)for i in rap:if(i notin sing and i notin dance):
only_one.append(i)print('只选了一学科的人数为:{},分别是{}'.format(len(only_one),only_one))
(4)求只选了两门学科的学生的数量和对应的名字
only_two =[]for i in sing:if i in rap and i notin dance:
only_two.append(i)elif i in rap and i notin dance:
only_two.append(i)for j in dance:if j in rap and j notin sing:
only_two.append(j)print('只选了两学科的人数为:{},分别是{}'.format(len(only_two),only_two))
(5)求选了三门学科的学生的数量和对应的名字
only_all =[]for i in sing:if i in dance and i in rap:
only_all.append(i)print('选三门学科的人数为:{},分别是{}'.format(len(only_all), only_all))
六、声明一个列表,在列表种保存6个学生信息
students =[{'name':'张三','age':18,'score':98,'tel':'15623755668','gender':'female'},{'name':'李四','age':17,'score':95,'tel':'15623755666','gender':'male'},{'name':'王五','age':21,'score':98,'tel':'15623755678','gender':'male'},{'name':'张飞','age':18,'score':58,'tel':'15623755689','gender':'female'},{'name':'jack','age':23,'score':52,'tel':'15623753456','gender':'female'},{'name':'wilson','age':16,'score':89,'tel':'15623753458','gender':'unknown'}]
1、统计不及格学生的个数
count =0# 空瓶for student in students:if student['score']<60:
count +=1print("不及格学生的个数是:%s"% count)
2、打印不及格学生的名字和对应的成绩
count =0# 空瓶for student in students:if student['score']<60:
count +=1print("{}不及格,分数是{}".format(student['name'], student['score']))
3、统计未成年学生的个数
count =0# 空瓶for student in students:if student['age']<18:
count +=1print("未成年的人数是:%s"% count)
4、打印手机尾号是8的学生的姓名
for i in students:if i['tel'][-1]=='8':print(i['name'])
5、打印最高分和对应学生的名字
max_score = students[0]['score']# 假设第0个学生成绩最高
max_index =0# 假设最高分学生的成绩下标为0for student in students:if student['score']> max_score:# 遍历 如果有比最高分还高 那那个数就是最大的
max_score = student['score']# max_index = i # 修改最高分的同时,也需要修改下标for student in students:if student['score']== max_score:print('最高分是%s'% student['name'])
6、删除性别不明的所有学生
for i in students[:]:ifnot i.get('gender'):
students.remove(i)print(students)
7、将列表按学生成绩从大到校排序
for i inrange(len(students)):for j inrange(len(students)-1-i):if students[j]['score']<students[j+1]['score']:
students[j],students[j+1]= students[j+1],students[j]print(students)