赞
踩
- path = '/data/bigfiles/' # 路径 不然会出现报错
- def read_file(file, m):
- """读文件中的学校名到列表中,返回前m个记录的学校集合"""
- with open(path + file, 'r',encoding='utf-8') as f:
- lines=[]
- for i in range(m):
- line=f.readline().strip("\n")
- lines.append(line.split()[1])#对一行的字符串内容分割成列表,取这个列表索引位置1的元素
- return lines
-
-
- def either_in_top(alumni, soft):
- """接收两个排行榜前m高校名字集合,
- 获得在这两个排行榜中均在前m个记录的学校名,按照学校名称排序,
- 返回排序后的列表
- """
- result=[]
- for i in range(len(alumni)):
- if alumni[i] in soft:#如果同时在两个表中都有这个学校
- result.append(alumni[i])
- result.sort()#升序排序
- return result
-
-
- def all_in_top(alumni, soft):
- """接收两个排行榜前m高校名字集合,
- 获得在两个榜单中名列前m的所有学校名,按照学校名称排序,
- 返回排序后的列表
- """
- result=[]
- result.extend(alumni)#列表合并alumni
- result.extend(soft)#列表合并soft
- result=list(set(result))#列表去重
- result.sort()#升序排序
- return result
-
-
- def only_alumni(alumni, soft):
- """接收两个排行榜前m高校名字集合,
- 获得在alumni榜单中名列前m但soft榜单中未进前m的学校名,
- 按照学校名称排序,返回排序后的列表
- """
- result=[]
- for i in range(len(alumni)):
- if alumni[i] in soft:
- continue
- else:#如果在alumni榜单中名列前m但soft榜单中未进前m的学校名
- result.append(alumni[i])
- result.sort()#升序排序
- return result
-
-
- def only_once(alumni, soft):
- """接收两个排行榜前m高校名字集合,
- 获得在alumni和soft榜单中名列前m,但不同时出现在两个榜单的学校名,
- 按照学校名称排序,返回排序后的列表
- """
- result=[]
- for i in range(len(alumni)):
- if alumni[i] in soft:
- continue
- else:#如果在alumni榜单中名列前m但soft榜单中未进前m的学校名
- result.append(alumni[i])
- for i in range(len(soft)):
- if soft[i] in alumni:
- continue
- else:#如果在soft榜单中名列前m但alumni榜单中未进前m的学校名
- result.append(soft[i])
- result.sort()#升序排序
- return result
-
-
- def judge(n):
- if n in '1234':
- m = int(input())
- alumni_set = read_file('./alumni.txt', m)
- soft_set = read_file('./soft.txt', m)
- if n == '1':
- either_rank = either_in_top(alumni_set, soft_set)
- print(f'两榜单中均名列前{m}的学校:')
- print(either_rank)
- elif n == '2':
- all_rank = all_in_top(alumni_set, soft_set)
- print(f'两榜单名列前{m}的所有学校:')
- print(all_rank)
- elif n == '3':
- only_in_alumni_rank = only_alumni(alumni_set, soft_set)
- print(f'alumni中名列前{m},soft中未进前{m}的学校:')
- print(only_in_alumni_rank)
- elif n == '4':
- alumni_soft_rank = only_once(alumni_set, soft_set)
- print(f'不同时出现在两个榜单前{m}的学校:')
- print(alumni_soft_rank)
- else:
- print('Wrong Option')
-
-
- if __name__ == '__main__':
- num = input()
- judge(num)
-

- x = set('runoob')
- y = set('google')
- print(x) #重复的被删除
- print(y) #重复的被删除
- print(x&y) #返回一个新的集合,包括同时在集合 x 和y中的共同元素。
- print(x|y) #返回一个新的集合,包括集合 x 和 y 中所有元素。
- print(x^y) #返回一个新的集合,包括集合 x 和 y 的非共同元素。
- print(x-y) #返回一个新的集合,包括在集合 x 中但不在集合 y 中的元素。
'运行
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。