当前位置:   article > 正文

【头歌-Python】Python第八章作业(初级)_【头歌-python】python第八章作业(初级)

【头歌-python】python第八章作业(初级)

第1关:统计字母数量

  1. import string
  2. n = int(input())
  3. dic = {}
  4. f = open("step2/The Old Man and the Sea.txt", "r")
  5. i = 0
  6. for line in f:
  7. if i>=n:
  8. break
  9. else:
  10. i+=1
  11. for word in line.lower():
  12. if word.isalpha():
  13. dic[word] = dic.get(word, 0)+1
  14. list = sorted(dic.items(), key=lambda x: (-x[1], x[0]))
  15. for x1,x2 in list:
  16. print("{} 的数量是 {:>3} 个".format(x1, x2))
  17. for c in string.ascii_lowercase:
  18. if c not in dic:
  19. print(f"{c} 的数量是 {0:>3} 个")

第2关:统计文章字符数

  1. with open('step3/The Great Learning.txt', 'r', encoding='utf-8') as f:
  2. n =int(input())
  3. text = ''.join(f.readlines()[:n])
  4. sum1 = len(text)
  5. sum2 = len(set(text))
  6. print(sum1, sum2)

第3关:查询高校信息

  1. with open('step4/university.csv','r',encoding='utf-8') as Uname:
  2. ls = Uname.readlines()
  3. name = input()
  4. print(ls[0].strip())
  5. for i in ls:
  6. if name in i.split(','):
  7. print(i)
  8. break

第4关:查询高校名

  1. with open('step5/university.csv','r',encoding='utf-8') as Uname:
  2. ls = Uname.readlines()
  3. key = input()
  4. for i in ls:
  5. if key in i.split(',')[1]:
  6. print(i.split(',')[1])

第5关:通讯录读取   

  1. a = input()
  2. if a in 'AD':
  3. with open('step6//info.csv','r',encoding='utf-8') as f:
  4. if a=='A':
  5. print(f.read().replace(',',' '))
  6. elif a=='D':
  7. dic = {}
  8. ls = f.readlines()
  9. for i in ls:
  10. lst = i.strip().split(',')
  11. dic[lst[0]] = [lst[1],lst[2]]
  12. print(dic)
  13. else:
  14. print('ERROR')

第6关:JSON转列表     

  1. import json
  2. with open('step7/score1034.json', 'r', encoding='utf-8') as f:
  3. text = json.loads(f.read())
  4. ls = [['姓名', '学号', 'C', 'C++', 'Java', 'Python', 'C#', '总分']]
  5. for i in text:
  6. ls.append(list(i.values()))#获取字典中所有值
  7. n = int(input())
  8. print(ls[:n])

第7关:利用数据文件统计成绩

  1. with open('step8/成绩单.csv', 'r', encoding='utf-8') as f:
  2. n = int(input())
  3. ls = []
  4. av = [0,0,0,0,0,0]
  5. for line in f.readlines():
  6. ls.append(line.strip('\n').split(','))
  7. for j in range(3,9):
  8. av[j-3] = av[j-3]+int(line.split(',')[j])
  9. ls.sort(key = lambda x:int(x[9]))
  10. print('最低分{}分,最高分{}分'.format(ls[0][9], ls[-1][9]))
  11. print(ls[:n])#前n排数据
  12. if n <= len(ls):
  13. print(ls[(len(ls)-n):])#后n排数据
  14. else:
  15. print(ls)
  16. lst = []
  17. for j in range(0,6):
  18. lst.append(float(f'{int(av[j])/len(ls):.2f}'))
  19. print(lst)

   第8关:研究生录取数据分析A

  1. def one(filename):
  2. with open(filename, 'r') as f:
  3. ls = []
  4. for line in f.readlines()[1:]:
  5. res = line.strip().split(',')
  6. if eval(res[-1]) >= 0.8:
  7. ls.append(res)
  8. return ls
  9. def two(filename):
  10. with open(filename, 'r') as f:
  11. ls1 = []
  12. ls2 = []
  13. for line in f.readlines()[1:]:
  14. res = line.strip().split(',')
  15. if eval(res[-1]) >= 0.9:
  16. ls1.append(res)
  17. elif eval(res[-1]) <= 0.7:
  18. ls2.append(res)
  19. return ls1, ls2
  20. def main():
  21. n = input()
  22. filename = 'step9//admit2.csv'
  23. if n == '1':
  24. ls = one(filename)
  25. count = 0
  26. for i in ls:
  27. if eval(i[1]) >= 4:
  28. count += 1
  29. print('Top University in >=80%:{:.2f}%'.format(count / len(ls) * 100))
  30. elif n == 'Research':
  31. ls1, ls2 = two(filename)
  32. count = 0
  33. for i in ls1:
  34. if i[-4] == '1' :
  35. count+=1
  36. print('Research in >=90%:{:.2f}%'.format(count / len(ls1) * 100))
  37. count = 0
  38. for i in ls2:
  39. if i[-4] == '1' :
  40. count+=1
  41. print('Research in <=70%:{:.2f}%'.format(count / len(ls2) * 100))
  42. elif n == '2':
  43. ls = [eval(i[3]) for i in one(filename)]
  44. print('TOEFL Average Score:{:.2f}'.format(sum(ls) / len(ls)))
  45. print('TOEFL Max Score:{:.2f}'.format(max(ls)))
  46. print('TOEFL Min Score:{:.2f}'.format(min(ls)))
  47. elif n == '3':
  48. ls = [eval(i[4]) for i in one(filename)]
  49. print('CGPA Average Score:{:.3f}'.format(sum(ls) / len(ls)))
  50. print('CGPA Max Score:{:.3f}'.format(max(ls)))
  51. print('CGPA Min Score:{:.3f}'.format(min(ls)))
  52. else:
  53. print("ERROR")
  54. main()

第9关:图书数据分析(A)

  1. with open('step10/CBOOK.csv', 'r') as f:
  2. ls = [line.strip().split(',') for line in f.readlines()[1:]]
  3. n = input()
  4. if n == 'record':
  5. print(len(ls))
  6. elif n == 'rank':
  7. num = input()
  8. for i in ls:
  9. if i[0] == num:
  10. for j in i:
  11. print(j)
  12. elif n=='maxcomment':
  13. res = sorted(ls,key=lambda x:eval(x[5][:-3]),reverse = True)
  14. for i in res[:10]:
  15. print(i[1],i[5])
  16. elif n=='maxname':
  17. num =int(input())
  18. res = sorted(ls,key=lambda x:len(x[1]),reverse = True)
  19. for i in res[:num]:
  20. print(i[1])
  21. else:
  22. print('无数据')

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/616873
推荐阅读
相关标签
  

闽ICP备14008679号