赞
踩
- def read_file(file):
- """@参数 file:文件名,字符串类型
- 读文件中的学校名到列表中,返回排名前10学校集合,集合类型。
- """
- s = set()
- f = open(file, 'r', encoding='utf-8')# 打开要去掉空行的文件
- l = f.readlines()
- for j in l:
- if file == "soft.txt":
- j = j.split(" ")
- j[1] = j[1][:-1]
- else:
- j = j.split(" ")
- if int(j[0])<=10:
- s.add(j[1])
- else:
- break
- return s
-
-
- def either_in_top(alumni, soft):
- """
- @参数 alumni:alumni大学排行榜中排名前10的学校的集合,集合类型
- @参数 soft:soft大学排行榜中排名前10的学校的集合,集合类型
- 接收两个排行榜前10高校名字集合,返回在这两个排行榜中均名列前10的学校名。
- """
- return (alumni & soft)
-
-
- def all_in_top(alumni, soft):
- """
- @参数 alumni:alumni大学排行榜中排名前10的学校的集合,集合类型
- @参数 soft:soft大学排行榜中排名前10的学校的集合,集合类型
- 接收两个排行榜前10高校名字集合,
- 返回在两个榜单中名列前10的所有学校名。
- """
- return (alumni | soft)
-
-
- def only_alumni(alumni, soft):
- """
- @参数 alumni:alumni大学排行榜中排名前10的学校的集合,集合类型
- @参数 soft:soft大学排行榜中排名前10的学校的集合,集合类型
- 接收两个排行榜前10高校名字集合,返回在alumni榜单中名列前10但soft榜单中未进前10的学校名。
- """
-
- return (alumni.difference(soft))
-
-
- def only_one(alumni, soft):
- """
- @参数 alumni:alumni大学排行榜中排名前10的学校的集合,集合类型
- @参数 soft:soft大学排行榜中排名前10的学校的集合,集合类型
- 接收两个排行榜前10高校名字集合,返回在alumni和soft榜单中名列前10,
- 但不同时出现在两个榜单的学校名。
- """
- either_rank = either_in_top(alumni_set, soft_set)
- all_rank = all_in_top(alumni_set, soft_set)
- return (all_rank.symmetric_difference(either_rank))
-
-
- if __name__ == '__main__':
- alumni_set = read_file('alumni.txt') #路径像我一样就要.py文件和.txt文件放在相同目录下
- soft_set = read_file('soft.txt')
- either_rank = either_in_top(alumni_set, soft_set)
- all_rank = all_in_top(alumni_set, soft_set)
- only_in_alumni_rank = only_alumni(alumni_set, soft_set)
- alumni_soft_rank = only_one(alumni_set, soft_set)
- print(either_in_top(alumni_set, soft_set))
- print("\n")
- print(f'两榜单中均名列前10的学校{either_rank}')
- print("\n")
- print(f'两榜单名列前10的所有学校{all_rank}')
- print("\n")
- print(f'alumni中名列前10,soft中未进前10的学校{only_in_alumni_rank}')
- print("\n")
- print(f'不同时出现在两个榜单前10的学校{alumni_soft_rank}')
-
-
-
-
-
-
-
-
-

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。