使用python语言创建空列表score,按学号顺序(由小到大)保存多个学生一门课程的考试成绩。调用列表操作的常用函数实现以下功能:
1)创建一个空列表score;
2)调用append()函数在score列表中依次追加10个数值(68,87,92,100,76,88,54,89,76,61);
3)输出score列表中第3个元素的数值;
4)输出score列表中第1~6个元素的值;
5)调用insert()函数,在score列表第3个元素之前添加数值59;
6)利用变量num保存数值76,调用count()函数,查询num变量值在score列表中出现的次数;
7)使用in查询score列表中是否有num变量值的考试成绩;
8)调用index()函数,查询score列表中成绩是满分的学生学号;
9)score列表中将59分加1分;
10)调用del()函数删除score列表中第1个元素;
11)调用len()函数获得score列表中元素的个数;
12)调用sort()函数,对列表中所有元素进行排序,输出考试的最高分和最低分;
13)调用reverse()函数,颠倒score列表中元素的顺序;
14)调用pop()函数删除score列表中尾部的元素,返回删除的元素;
15)score列表中追加数值88,并输出。调用remove()函数删除score列表中第一个数值88;
16)创建2个列表score1和score2,score1中包含数值2个元素值:80,61,score2中包含3个元素值:71,95,82,合并这两个列表,并输出全部元素;
17)创建score1列表,其中包含数值2个元素值:80,61,将score1中元素复制5遍保存在score2列表中,输出score2列表中全部元素。
1)创建一个空列表score;
2)调用append()函数在score列表中依次追加10个数值(68,87,92,100,76,88,54,89,76,61);
3)输出score列表中第3个元素的数值;
4)输出score列表中第1~6个元素的值;
5)调用insert()函数,在score列表第3个元素之前添加数值59;
6)利用变量num保存数值76,调用count()函数,查询num变量值在score列表中出现的次数;
7)使用in查询score列表中是否有num变量值的考试成绩;
8)调用index()函数,查询score列表中成绩是满分的学生学号;
9)score列表中将59分加1分;
10)调用del()函数删除score列表中第1个元素;
11)调用len()函数获得score列表中元素的个数;
12)调用sort()函数,对列表中所有元素进行排序,输出考试的最高分和最低分;
13)调用reverse()函数,颠倒score列表中元素的顺序;
14)调用pop()函数删除score列表中尾部的元素,返回删除的元素;
15)score列表中追加数值88,并输出。调用remove()函数删除score列表中第一个数值88;
16)创建2个列表score1和score2,score1中包含数值2个元素值:80,61,score2中包含3个元素值:71,95,82,合并这两个列表,并输出全部元素;
17)创建score1列表,其中包含数值2个元素值:80,61,将score1中元素复制5遍保存在score2列表中,输出score2列表中全部元素。
score=[] score.append(68) score.append(87) score.append(92) score.append(100) score.append(76) score.append(88) score.append(54) score.append(89) score.append(76) score.append(61) print(score[2]) print(score[0:6]) print(score) score.insert(2,59) print(score) num=76 print(score.count(num)) if num in score: print("有") print("学号:",score.index(100)) for i in range(0,len(score)): if score[i]==59: score[i]+=1 del score[0] print(score,"删除第一个元素",len(score)) score.sort() print(max(score)) print(min(score)) score.reverse() print(score) print(score.pop(len(score)-1)) score.append(88) print(score) score.remove(88) print(score) score1=[80,61] score2=[72,95,82] sldfj=score1+score2 print(sldfj) score2+=score1[:]*5 print(score2)
1.两个 3 行 3 列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵: X = [[12,7,3], [4 ,5,6], [7 ,8,9]] Y = [[5,8,1], [6,7,3], [4,5,9]]
答
x=[[12,7,3],[4,5,6,],[7,8,9]] y=[[5,8,1],[6,7,3,],[4,5,0]] b=[] for i in range(0,len(x)): a = [] for s in range(0,len(x[i])): a.append(x[i][s]+y[i][s]) else: b.append(a)