from collections import defaultdict import re with open('./static/年级学院专业.txt','r') as f: academys = defaultdict(dict) for line in f.readlines(): data = line.split('@') academy = data[1] major = data[2].strip() if major not in academys[academy]: academys[academy][major] = [] s = re.sub("\d+级",'',data[3].strip()) s = re.sub("\d+",'',s) academys[academy][major].append(s) else: s = re.sub("\d+级",'',data[3].strip()) s = re.sub("\d+",'',s) academys[academy][major].append(s) for academy_ in academys.keys(): for key,item in academys[academy_].items(): academys[academy_][key] = set(item) print('学院数量:',len(academys.keys())) for academy in academys.keys(): print(academy,'有 {} 个专业'.format(len(academys[academy].keys())))