【头歌-Python】Python第七章作业
洛水入长安 2024-06-29 10:35:04 阅读 75
第1关:字符串去重排序
n = input()
a = list(set(n))
a.sort()
a = ''.join(a).strip()
print(a)
第2关:列表去重
a = input().split(',')
c=[]
[c.append(i) for i in a if i not in c]
print(c)
第3关:猜年龄
for i in range(10,1000):
if(pow(i,3)>=10000) or (pow(i,4)>=1000000):
break
if(pow(i,4)<100000):
continue
str1 = str(pow(i,3))+str(pow(i,4))
a = set(str1)
if len(a)==10:
print(i)
break
第4关:集合的属性、方法与运算
n = int(input())
name = input()
MySet = set(name.split(' '))
for i in range(n):
ls = input().split(' ')
if ls[0]=='print':
print(sorted(list(MySet)))
elif ls[0]=='del':
MySet.discard(ls[1])
elif ls[0]=='clear':
MySet.clear()
elif ls[0]=='add':
MySet.add(ls[1])
elif ls[0]=="update":
MySet.update(set(ls[1:]))
第5关:集合介绍
def average(array):
a = set(array)
length = len(a)
sumnum = sum(a)
return sumnum/length
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 = int(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 if x not in sale2018]+[x for x in sale2018 if x not in sale2019]))
第7关:集合添加元素
N = int(input())
s = set()
for i in range(N):
s.add(input())
print(len(s))
第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}
sum = 0
num = 0
while True:
a = input()
if a=='-1':
print(f'{sum/num:.2f}')
break
else:
b = int(input())
sum = sum+b*dic[a]
num = num+b
第10关:通讯录(MOD)
print({'张自强': ['12652141777', '材料'], '庚同硕': ['14388240417', '自动化'], '王岩': ['11277291473', '文法']})
print()
dict = {'张自强': ['12652141777', '材料'], '庚同硕': ['14388240417', '自动化'], '王岩': ['11277291473', '文法']}
# 学生通讯录管理系统主界面
def Menu():
print("欢迎使用PYTHON学生通讯录")
print("1:添加学生")
print("2:删除学生")
print("3:修改学生信息")
print("4:搜索学生")
print("5:显示全部学生信息")
print("6:退出并保存")
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)
def delstuInof():
name = input()
del dict[name]
print('Success')
print(dict)
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)
def seckstuIofo():
name = input()
print(dict[name])
def showstuInfo():
print(dict)
def exitSystem():
pass
if __name__ == '__main__':
Menu()
num = int(input())
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")
第11关:字典增加元素
dict1 = {'赵小明': '13299887777', '特明朗': '814666888', '普希京': '522888666', '吴小京': '13999887777'}
name = input()
phonenum = input()
if name in dict1:
print('您输入的姓名在通讯录中已存在')
else:
dict1[name] = phonenum
for i in dict1:
print(i + ':' + dict1[i])
第12关:字典的属性、方法与应用
n = int(input())
name = input().split(',')
phonenum = input().split(',')
dic = dict(zip(name, phonenum))
for i in range(n):
list = input().split(' ')
if list[0]=='key':
print([x for x in dic])
elif list[0]=='value':
print([dic[x] for x in dic])
elif list[0]=='print':
print(dic)
elif list[0]=='clear':
dic.clear()
elif list[0]=='add':
dic[list[1]] = list[2]
elif list[0]=='update':
dic.update({list[1] : list[2]})
elif list[0]=='del':
if list[1] in dic:
del dic[list[1]]
else:
print('键不存在')
第13关:查询省会
capitals = {'湖南':'长沙','湖北':'武汉','广东':'广州','广西':'南宁','河北':'石家庄','河南':'郑州','山东':'济南','山西':'太原','江苏':'南京','浙江':'杭州','江西':'南昌','黑龙江':'哈尔滨','新疆':'乌鲁木齐','云南':'昆明','贵州':'贵阳','福建':'福州','吉林':'长春','安徽':'合肥','四川':'成都','西藏':'拉萨','宁夏':'银川','辽宁':'沈阳','青海':'西宁','海南':'海口','甘肃':'兰州','陕西':'西安','内蒙古':'呼和浩特','台湾':'台北','北京':'北京','上海':'上海','天津':'天津','重庆':'重庆','香港':'香港','澳门':'澳门'}
while True:
name = input()
if len(name)==0:
break
else:
if name not in capitals:
print('输入错误')
else:
print(capitals[name])
第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(my_str):
"""将句子里的's 用 is 替换,n't 用 not 替换。
所有符号替换为空格,再根据空格切分为列表。
返回列表。
"""
my_str = my_str.replace("n't", ' not')
my_str = my_str.replace("'s", ' is')
for x in string.punctuation:
my_str = my_str.replace(x, ' ')
lst = my_str.split(' ')
return lst
def query_words(lst,dic):
"""接收列表和字典为参数,对列表中的单词进行遍历,
将单词字母转小写,到字典中查询单词的中文意义并输出。
若单词在字典中不存在,输出'自己猜'。
"""
for word in lst:
if word == '':
continue
word = word.lower()
print(word, 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 Menu():
print("欢迎使用PYTHON学生通讯录")
print("1:添加学生")
print("2:删除学生")
print("3:修改学生信息")
print("4:搜索学生")
print("5:显示全部学生信息")
print("6:退出并保存")
def addstuInof():
name = input()
if name in dict:
print('Fail')
print(dict)
else:
sno = input()
spet = input()
dict[name] = [sno,spet]
print('Success')
print(dict)
def delstuInof():
name = input()
if len(name) == 0:
print("ERROR")
else:
del dict[name]
print('Success')
print(dict)
def modifystuInfo():
name = input()
if name in dict:
stu_num = input()
zuanye = input()
dict[name] = [stu_num,zuanye]
print('Success')
print(dict)
else:
print('Fail')
print(dict)
def seckstuIofo():
name = input()
print(dict[name])
def showstuInfo():
print(dict)
def exitSystem():
pass
if __name__ == '__main__':
Menu()
num = int(input())
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")
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。