from functools import reduce for student in studens: result = (reduce(lambda x,y:x+y,list(student.values())[1:],0))//3 student['avg'] = result print(studens)
from functools import reduce for student in studens: result = (reduce(lambda x,y:x+y,list(student.values())[1:],0))//3 student['avg'] = result print(studens)
list3 = [10, 20, 34, 56, 90] item = int(input("请输入一个整数:")) for index inrange(len(list3)): if list3[index] > item: list3.insert(index,item) break else: list3.append(item) print(list3)
两个 3 行 3 列的矩阵,实现其对应位置的数据相加,并返回一个新矩阵:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
X = [[12,7,3], [4 ,5,6], [7 ,8,9]]
Y = [[5,8,1], [6,7,3], [4,5,9]]
X + Y 为: [ [17, 15, 4], [10, 12, 9], [11, 13, 18] ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
X = [ [12,7,3], [4 ,5,6], [7 ,8,9] ]
Y = [ [5,8,1], [6,7,3], [4,5,9] ] W = [] for index1 inrange(len(X)): Z = [] for index2 inrange(len(X[index1])): Z.append(X[index1][index2] + Y[index1][index2]) W.append(Z) print(W)
num_list = [1,2,3,4] count = 0 for i inrange(len(num_list)): for j inrange(len(num_list)): for k inrange(len(num_list)): a = num_list[i] b = num_list[j] c = num_list[k] if a != b and a != c and b != c: print(num_list[i] * 100 + num_list[j] * 10 + num_list[k]) count += 1 print("一共可以组成%s个不重复的三位数" % count)
import math for i inrange(10000): x = int(math.sqrt(i + 100)) y = int(math.sqrt(i + 100 + 168)) if (x * x == i + 100) and (y * y == i + 100 + 168): print(i)
输出 9*9 乘法口诀表。(需要对齐)
1 2 3 4
for i inrange(1,10): for j inrange(1,i+1): print(i,"*",j,"=",i*j,end='\t') print("")
n1 = 1 n2 = 1 n = int(input("请输入是第几个月:")) if n == 1or n == 2: print(n,"个月的兔子数是:",1) else : for i inrange(n-3): n1,n2=n2,n1+n2 print(n,"个月的兔子数是:",n1+n2)
a = input("请输入用户名:") flag = False if'A' <= a[0] <= 'Z': flag = True else: print('用户名不合法') nums = 0 if flag: for i in a: if'a' <= i <= 'z'or'A' <= i <= 'Z'or'0' <= i <= '9': if'0' <= i <= '9': nums+=1 continue else: print('用户名不合法') break else: if nums >=1: print('用户名合法') else: print('用户名不合法')
5. 输入一个字符串,将字符串中所有的数字字符取出来产生一个新的字符串
例如:输入**’abc1shj23kls99+2kkk’** 输出:**’123992’**
1 2 3 4 5 6
str1 = 'abc1shj23kls99+2kkk' new_str1 = '' for i in str1: if'0' <= i <= '9': new_str1+=i print(new_str1)
a = input("请输入用户名:") flag = False if'A' <= a[0] <= 'Z': flag = True else: print('用户名不合法') nums = 0 if flag: for i in a: if'a' <= i <= 'z'or'A' <= i <= 'Z'or'0' <= i <= '9': if'0' <= i <= '9': nums+=1 continue else: print('用户名不合法') break else: if nums >=1: print('用户名合法') else: print('用户名不合法')
5. 输入一个字符串,将字符串中所有的数字字符取出来产生一个新的字符串
例如:输入**’abc1shj23kls99+2kkk’** 输出:**’123992’**
1 2 3 4 5 6
str1 = 'abc1shj23kls99+2kkk' new_str1 = '' for i in str1: if'0' <= i <= '9': new_str1+=i print(new_str1)
count = 0 for item in list1: if item['score']<60: count+=1 print("不及格学生个数为:",count)
1. 2打印不及格学生的名字和对应的成绩
1 2 3
for item in list1: if item['score']<60: print(item['姓名'],item['score'])
1.3 打印手机尾号是8的学生的名字
1 2 3
for item in list1: ifint(item['tel']) % 10 == 8: print(item['姓名'],item['tel'])
1.4 打印最高分和对应的学生的名字
1 2 3 4 5 6 7
max_score = 0 for item in list1: if item['score'] > max_score: max_score = item['score'] for i in list1: if i['score'] == max_score: print(i['姓名'],i['score'])
1.5 删除性别不明的所有学生
1 2 3 4
for item in list1: if item['sex'] == '不明': list1.remove(item) print(list1)
1.6 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
1 2 3 4 5 6 7 8 9 10 11
for x inrange(len(list1)): i = 0 j = 1 while j<len(list1): if list1[i]['score'] < list1[j]['score']: k = list1[j] list1[j] = list1[i] list1[i] = k i+=1 j+=1 print(list1)
2.用三个集合表示三门学科的选课学生姓名(一个学生可以同时选多门课)
1 2 3
chinese = {'小明', '小花', '张三', '王五','赵六'} english = {'小翠', '李四', '张三', '王五','赵六'} math = {'小明', '李四', '王五','赵六'}
2.1 求选课学生总共有多少人
1
print('选课总人数为:',len(chinese|english|math))
2.2 求只选了第一个学科的人的数量和对应的名字
1 2 3 4 5 6 7
count = 0 set1 = set() for item in chinese: if item notin english and item notin math: count+=1 set1.add(item) print('只选了第一个学科的数量为:',count,'名字有:',set1)
count =0 set3 = set() for item in chinese: if (item in english and item notin math) or (item in math and item notin english): count+=1 set3.add(item) print('只选了两门学科的人数为:',count,'名字有:',set3)
2.5 求选了三门学生的学生的数量和对应的名字
1 2 3 4 5 6 7
count =0 set4 = set() for item in chinese: if item in english and item in math: count+=1 set4.add(item) print("选了三门学生的数量为:",count,'名字有:',set4)