将用户输入用空格分隔的一系列地名创建集合MySet输入一个正整数 n你将被要求读入 n 个输入输入形式如下所示每得到一个输入后根据输入进行操作。add name # 在集合中加入元素nameprint # 将集合转为列表按元素升序排序后输出列表del name # 删除集合中的元素name当name不存在时不能引发错误u
n = int(input()) MySet = set() for i in range(n): operation = input().split() if operation[0] == 'add': MySet.add(operation[1]) elif operation[0] == 'print': print(sorted(list(MySet))) elif operation[0] == 'del': if operation[1] in MySet: MySet.remove(operation[1]) elif operation[0] == 'update': name = operation[1].split(',') MySet.update(set(name)) elif operation[0] == 'clear': MySet.clear()
原文地址: http://www.cveoy.top/t/topic/g1iA 著作权归作者所有。请勿转载和采集!