赞
踩
第1关 字符串去重排序
- n = input()
- a = list(set((n)))
- a.sort()
- a = ''.join(a).strip()
- print(a)
第2关 列表去重
- n = input().split(',')
- a = []
- [a.append(i) for i in n if i not in a]
- print(a)
第3关 猜年龄
- dig = (1, 2, 3, 4, 5, 6, 7, 8, 9, 0)
- age1 = 0
- age2 = 0
- while age1 **3 < 10000:
- age1 += 1
- while True:
- b = age2**4
- if b >= 100000 and b < 1000000:
- break
- age2 += 1
-
- for i in range(age2,age1):
- num1 = i**3
- num2 = i**4
- set1 = set()
- for j in str(num1):
- set1.add(j)
- for k in str(num2):
- set1.add(k)
- if len(set1) == len(dig):
- print(i)
第4关 集合的属性、方法与运算
- n = int(input()) # 输入一个正整数 n
- name = input() # 吉林,湖北,湖南
- MySet = set(name.split())
- MyList = name.split()
- for i in range(n):
- ls = input().split() # 输入命令及参数,之间用空格分隔
- if ls[0] == 'print': # 如要输入的命令是“print”,输出字典
- print(sorted(list(MySet)))
- elif ls[0] == 'update': # 如要输入的命令是“update”,更新ls[1]表示的键对应的值
- MySet.update(set(ls[1:]))
- elif ls[0] == 'add': # 如要输入的命令是“add”,增加一个键值对,题目确保输入的键在字典中不存在
- MySet.add(ls[1])
- elif ls[0] == 'del': # 如要输入的命令是“del”,删除字典中指定的键值对,键不存在时返回“键不存在”
- MySet.discard(ls[1])
- elif ls[0] == 'clear': # 如要输入的命令是“clear”,清空字典中全部元素
- MySet.clear()
第5关 集合介绍
- def average(array):
- # 你的代码写在这里
- return sum(set(array))/len(set(array))
-
- if __name__ == '__main__':
- arr = list(map(int, input().split()))
- result = average(arr)
- print(result)
第6关 手机销售统计
- with open('/data/bigfiles/sale2019.csv', 'r', encoding='utf-8') as data2019:
- sale2019 = [line.strip().split(',')[0] for line in data2019]
- with open('/data/bigfiles/sale2018.csv', 'r', encoding='utf-8') as data2018:
- sale2018 = [line.strip().split(',')[0] for line in data2018]
- n = input()
- if n =='1':
- print(sorted(sale2019))
- print(sorted(sale2018))
- if n =='2':
- print(sorted([x for x in sale2019 if x in sale2018]))
- if n =='3':
- print(sorted(sale2019 + [x for x in sale2018 if x not in sale2019]))
- if n =='4':
- print(sorted([x for x in sale2019 if x not in sale2018]))
- if n =='5':
- print(sorted([x for x in (sale2019 + sale2018) if x not in[x for x in sale2019 if x in sale2018]]))
第7关 集合添加元素
- n = int(input())
- lis = []
- for i in range(n):
- a = input()
- lis.append(a)
- print(len(set(lis)))
第8关 列表嵌套字典的排序
- n = int(input())
- list = []
- for i in range(n):
- dic = {}
- name_age = input().split()
- dic.update({'name':name_age[0], 'age':int(name_age[1])})
- list.append(dic)
- print(sorted(list,key = lambda x : x['age']))
- print(sorted(list,key = lambda x : x['name']))
第9关 绩点计算
- dic = {'A':4.0,'A-':3.7,'B+':3.3,'B':3.0,'B-':2.7,'C+':2.3,'C':2.0,'C-':1.5,'D':1.3,'D-':1.0,'F':0}
- s = 0
- n = 0
- while True:
- a = input()
-
- if a != '-1':
- b = int(input())
- s += dic[a] * b
- n += b
- else:
- print(f'{s/n:.2f}')
- break
第10关 通讯录(MOD)
- print({'张自强': ['12652141777', '材料'], '庚同硕': ['14388240417', '自动化'], '王岩': ['11277291473', '文法']})
- print()
- dict = {'张自强': ['12652141777', '材料'], '庚同硕': ['14388240417', '自动化'], '王岩': ['11277291473', '文法']}
- # 学生通讯录管理系统主界面
- def showMenu():
- print("欢迎使用PYTHON学生通讯录")
- print("1:添加学生")
- print("2:删除学生")
- print("3:修改学生信息")
- print("4:搜索学生")
- print("5:显示全部学生信息")
- print("6:退出并保存")
-
-
- # 选择输入的功能
- def getSelcet():
- selectNum = int(input())
- return selectNum
-
-
- # 实现序号1:添加学生信息
- def addstuInof():
- name = input()
- if len(name) ==0:
- print("ERROR")
- else:
- stu_num = input()
- zuanye = input()
- dict[name] = [stu_num,zuanye]
- print('Success')
- print(dict)
-
- # 实现序号2:删除学生信息
-
- def delstuInof():
- name = input()
- del dict[name]
- print('Success')
- print(dict)
-
- # 实现序号3:修改学生信息
- def modifystuInfo():
- name = input()
- if name in dict:
- stu_num = input()
- zuanye = input()
- dict[name] = [stu_num,zuanye]
- print('Success')
- print(dict)
- else:
- print('No Record')
- print(dict)
-
- # 实现序号4:搜索学生信息
- def seckstuIofo():
- name = input()
- print(dict[name])
-
-
- # 实现序号5:显示全部学生信息
- def showstuInfo():
- print(dict)
-
-
- # 实现序号6 退出显示管理系统
- def exitSystem():
- pass
-
- # main主函数
- def main():
- showMenu()
- num = getSelcet()
- if num == 1:
- addstuInof()
- elif num == 2:
- delstuInof()
- elif num == 3:
- modifystuInfo()
- elif num == 4:
- seckstuIofo()
- elif num == 5:
- showstuInfo()
- elif num == 6:
- exitSystem()
- print("ERROR")
-
- main()
第11关 字典增加元素
- dict1 = {'赵小明': '13299887777', '特明朗': '814666888', '普希京': '522888666', '吴小京': '13999887777'}
- a = input()
- b = input()
- if a not in dict1:
- dict1[a] = b
- for i in dict1:
- print(i+':'+dict1[i])
- else:
- print('您输入的姓名在通讯录中已存在')
第12关 字典的属性、方法与应用
- n = int(input())
- name = input().split(',')
- telnumber = input().split(',')
- dic = dict(zip(name, telnumber))
- for i in range(n):
- order = input().split()
- if order[0] == 'key':
- print([x for x in dic])
- elif order[0] == 'value':
- print([dic[x] for x in dic])
- elif order[0] == 'print':
- print(dic)
- elif order[0] == 'clear':
- dic.clear()
- elif order[0] == 'add':
- dic[order[1]] = order[2]
- elif order[0] == 'update':
- dic.update({order[1] : order[2]})
- elif order[0] == 'del' :
- if order[1] in dic:
- del dic[order[1]]
- else:
- print('键不存在')
第13关 查询省会
- capitals = {'湖南': '长沙', '湖北': '武汉', '广东': '广州', '广西': '南宁', '河北': '石家庄', '河南': '郑州', '山东': '济南', '山西': '太原', '江苏': '南京',
- '浙江': '杭州', '江西': '南昌', '黑龙江': '哈尔滨', '新疆': '乌鲁木齐', '云南': '昆明', '贵州': '贵阳', '福建': '福州', '吉林': '长春',
- '安徽': '合肥', '四川': '成都', '西藏': '拉萨', '宁夏': '银川', '辽宁': '沈阳', '青海': '西宁', '海南': '海口', '甘肃': '兰州', '陕西': '西安',
- '内蒙古': '呼和浩特', '台湾': '台北', '北京': '北京', '上海': '上海', '天津': '天津', '重庆': '重庆', '香港': '香港', '澳门': '澳门'}
- lis1 = []
- lis2 = []
- while True:
- city = input()
- if len(city) == 0:
- break
- else:
- lis1.append(city)
- for i in lis1:
- if i in capitals:
- lis2.append(capitals[i])
- else:
- print('输入错误')
- for j in lis2:
- print(j)
第14关 英汉词典
- import string
-
- def read_to_dic(filename):
- """读文件每行根据空格切分一次,作为字典的键和值添加到字典中。
- 返回一个字典类型。
- """
- my_dic = {}
- with open('/data/bigfiles/dicts.txt', 'r', encoding='utf-8') as f:
- date = f.readlines()
- for x in date:
- x = x.replace('生存,','生存 ') #之前打开数据集有点问题,在这里用代码修改了一下数据集
- x = x.strip().split(maxsplit=1)
- my_dic.update({x[0]: x[1]})
- return my_dic
-
- def sentence_to_lst(sentence):
- """将句子里的's 用 is 替换,n't 用 not 替换。
- 所有符号替换为空格,再根据空格切分为列表。
- 返回列表。
- """
- sentence = sentence.replace("n't", ' not')
- sentence = sentence.replace("'s", ' is')
- for x in string.punctuation:
- sentence = sentence.replace(x, ' ')
- sentence_lst = sentence.split()
- return sentence_lst
-
- def query_words(sentence_lst, my_dic):
- """接收列表和字典为参数,对列表中的单词进行遍历,
- 将单词字母转小写,到字典中查询单词的中文意义并输出。
- 若单词在字典中不存在,输出'自己猜'。
- """
- for word in sentence_lst:
- word = word.lower()
- print(word, my_dic.get(word, '自己猜'))
-
- if __name__ == '__main__':
- my_str = input()
- file = 'dicts.txt'
- dic = read_to_dic(file)
- lst = sentence_to_lst(my_str)
- query_words(lst, dic)
第15关 通讯录(添加)
- print({'张自强': ['12652141777', '材料'], '庚同硕': ['14388240417', '自动化'], '王岩': ['11277291473', '文法']})
- print()
- dict = {'张自强': ['12652141777', '材料'], '庚同硕': ['14388240417', '自动化'], '王岩': ['11277291473', '文法']}
- # 学生通讯录管理系统主界面
- def showMenu():
- print("欢迎使用PYTHON学生通讯录")
- print("1:添加学生")
- print("2:删除学生")
- print("3:修改学生信息")
- print("4:搜索学生")
- print("5:显示全部学生信息")
- print("6:退出并保存")
-
-
- # 选择输入的功能
- def getSelcet():
- selectNum = int(input())
- return selectNum
-
-
- # 实现序号1:添加学生信息
- def addstuInof():
- name = input()
- if name in dict:
- print('Fail')
- print(dict)
- else:
- stu_num = input()
- zuanye = input()
- dict[name] = [stu_num,zuanye]
- print('Success')
- print(dict)
-
- # 实现序号2:删除学生信息
-
- def delstuInof():
- name = input()
- if len(name) == 0:
- print("ERROR")
- else:
- del dict[name]
- print('Success')
- print(dict)
-
- # 实现序号3:修改学生信息
- def modifystuInfo():
- name = input()
- if name in dict:
- stu_num = input()
- zuanye = input()
- dict[name] = [stu_num,zuanye]
- print('Success')
- print(dict)
- else:
- print('No Record')
- print(dict)
-
- # 实现序号4:搜索学生信息
- def seckstuIofo():
- name = input()
- print(dict[name])
-
-
- # 实现序号5:显示全部学生信息
- def showstuInfo():
- print(dict)
-
-
- # 实现序号6 退出显示管理系统
- def exitSystem():
- pass
-
- # main主函数
- def main():
- showMenu()
- num = getSelcet()
- if num == 1:
- addstuInof()
- elif num == 2:
- delstuInof()
- elif num == 3:
- modifystuInfo()
- elif num == 4:
- seckstuIofo()
- elif num == 5:
- showstuInfo()
- elif num == 6:
- exitSystem()
- print("ERROR")
-
- main()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。