python练习(4)

一、求列表中的最大数和下标

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 = 0
while i < len(nums):
    if nums[i] > x:
        x = nums[i]  # 找到后给x
        index = i    # 记录下来
    i += 1
print('发现的最大的数是‰d, 他的下标是‰d' % (x, index))

二、删除列表里面的空字符串

words2 = ['hello', '', 'good', 'yes', 'ok', '']
i = 0
while i < len(words2):
    if words2[i] == '':
        words2.remove(words2[i])
        i -= 1   # 删除一个空字符串  下标也要剪掉
    i += 1
print(words2)

三、一个学校,有三个办公室,现在有8位老师等待分配工位,请编写程序完成分配

teachers = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
rooms = [[], [], []]
for i, room in enumerate(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] += 1
    else:
        char_count[char] = 1  # 不在里面就是1
print(char_count)

# 第二种  拿个数
for char in chars:
    if char not in char_count:
        char_count[char] == char_count[char]
print(char_count)

五、用三个元组表示三门学科的选课学生姓名(一个学生可以同时选多门课程)

sing = ('李白', '白居易', '李清照', '杜甫', '小苗', '孟浩然')
dance = ('韩红', '蔡徐坤', '李诞', '苗苗', '李涛', '杜甫', '小苗')
rap = ('小蔡', '蔡徐坤', '杜甫', '小苗', '小涛')

(1)求选课学生总共有多少人

zong = (*sing, *dance, *rap)  # 合并元组
cong = set(zong)  # 去重
print("选课学生总共有:", len(cong))

(2)求只选了第一个学科的人的数量和对应的名字

one_class = []
for x in sing:
    if x not in dance and rap:
        one_class.append(x)
print('求只选了第一个学科的人的数量:{}和对应的名字:{}'.format(len(one_class), one_class))

(3)求只选了一门学科的学生的数量和对应的名字

only_one = []
for i in sing:
    if (i not in dance and i not in rap):
        only_one.append(i)
for j in dance:
    if (i not in sing and i not in rap):
        only_one.append(i)
for i in rap:
    if (i not in sing and i not in 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 not in dance:
        only_two.append(i)
    elif i in rap and i not in dance:
        only_two.append(i)
for j in dance:
    if j in rap and j not in 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 += 1
print("不及格学生的个数是:%s" % count)

2、打印不及格学生的名字和对应的成绩

count = 0  # 空瓶
for student in students:
    if student['score'] < 60:
        count += 1
        print("{}不及格,分数是{}".format(student['name'], student['score']))

3、统计未成年学生的个数

count = 0  # 空瓶
for student in students:
    if student['age'] < 18:
        count += 1
print("未成年的人数是:%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  # 假设最高分学生的成绩下标为0
for 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[:]:
    if not i.get('gender'):  
        students.remove(i)
print(students)

7、将列表按学生成绩从大到校排序

for i in range(len(students)):
    for j in range(len(students)-1-i):
        if students[j]['score'] <students[j+1]['score']:
            students[j],students[j+1]= students[j+1],students[j]
print(students)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值