AC代码展示
fo=open("dir_50.txt ","r",encoding="utf8")
ls=[]
for line in fo:
index=[]
linels=[]
xuehao=""
wflag=False
line=line.replace("\'0\'","")
for c in line:
if c in '\'\_':
wflag=True
continue
if c in '\,\.\]':
wflag=False
index.append(len(xuehao))
continue
if wflag==False:
continue
if wflag==True:
xuehao+=c
index=set(index)
index=list(index)
index.sort()
index.insert(0,0)
for i in range(len(index)-1):
linels.append(eval(xuehao[index[i]:index[i+1]]))
ls.append(linels)
fo.close()
d={}
for item in ls:
key=item[-1]
item.pop(-1)
d[str(key)]=item
for key in d:
print("\'{}\':{}".format(key,d[key]))
ls2=list(d.items())
d2={}
for pair in ls2:
for item in pair[1]:
d2[str(item)]=d2.get(str(item),0)+1
sums=0
for key in d2:
print("\'{}\':{}".format(key,d2[key]))
sums+=d2[key]
print("实际参加测试的人数是:",len(d2))
print("人均被测次数是:{:.1f}".format(sums/len(d2)))
需要注意的点
轻风犯过的错误已经在其中标出,这道题有丶东西
关键点:
对学号和照片编号的提取,代码主体是遍历文件的循环,也比较难以理解,灵感来自论语文本处理写标记