Computer Level 2 python simple application questions review notes (1)


Next, let’s move on to simple word questions~ It feels like it’s getting more and more difficult, and even the length of the video has become more than ten minutes. Let’s work together and make a wish, make a wish,
I want to be excellent!
The code can be modified at will, subject to completing the program function.

1. Word frequency statistics: Enter a group of school types corresponding to Chinese universities on the keyboard, separated by spaces, in one line.

Code prompt framework:

txt = input("请输入类型序列:")
...
d = {
    
    }
...
ls = list(d.item())
ls.sort(key=lambda x:x[1],reverse=True)
for k in ls:
	print("{}:{}".format(k[0],k[1]))

Reference example

txt = input("请输入类型序列:")
a = txt.split()#将得到的txt文件分隔开 a = ['综合','理工',...]
d = {
    
    }
for i in range(len(a)):#词频统计
	d[a[i]] = d.get(a[i],0) + 1
	#第一次综合不存在则将0赋给综合,综合 = 0+1
	#第二次理工不存在则理工 = 0+1
	#第三次综合存在则综合+1...
ls = list(d.items()) #包含d这个字典键值对信息的列表
#print(ls)
ls.sort(key=lambda x:x[1], reverse=True) #按照数量排序
for k in ls:
	print("{}:{}".format(k[0],k[1]))

Of course, this word frequency statistics can also be memorized directly. Just write about this aspect directly later~ However, it is said that this is very complicated. It seems to be simpler to traverse directly. Interested friends can try it and won’t do it here. Let’s go straight to the next question

2. Find the maximum value, minimum value, and average score: Enter the name of the course Xiao Ming studied and test scores and other information on the keyboard. Use spaces to separate the information. Each course has one line. Press Enter on a blank line to end the entry.

This question is mainly divided into three parts:
1. Data input.
The requirement of this part is to maintain the input state until the input is completed after pressing Enter.
2. Data processing
and sorting function (dictionary as an auxiliary, and then using the list method for sorting), calculation Average score
3. Data output

data = input()
d = {
    
    }
while data:
	tem = data.split()
	d[tem[0]] = tem[1]
	data = input()
#排序 背背背
ls = list(d.items())
ls.sort(key = lambda x:x[1], reverse = True)
#计算平均分
ag = 0
for i in d.values():
	ag = ag + int(i)
ag = ag/len(ls)
print("最高分课程是{} {},最低分课程是{} {},平均分是{:.2f}".format(ls[0][0],ls[0][1],ls[-1][0],ls[-1][1],ag))

3、

Guess you like

Origin blog.csdn.net/m0_68165821/article/details/132949488