Format
Plain text
Post date
2019-04-22 23:21
Zeitraum der Veröffentlichung
Unbegrenzt
  1. #20190409文件读入作业
  2. f = open("white_wine.csv","r")
  3. out = open("output.txt","w")
  4. lines = f.readlines()
  5. quality=set()
  6. dic = {}
  7. fa = {}
  8. #001输出数据的前五行
  9. for i in range(0,5):
  10. lines[i]=lines[i].strip()
  11. print(lines[i])
  12. #002用集合查看葡萄酒的等级
  13. for line in lines:
  14. line = line.strip()
  15. if line[-1]>='0' and line[-1] <='9':
  16. quality.add(line[-1])
  17. print(quality)
  18. num = len(quality)
  19. print(num)
  20. #003将葡萄酒的品质的数量存到字典中,其中等级作为key,在该等级的作为value
  21. for i in quality:
  22. dic[i]=[]
  23. for line in lines:
  24. line = line.strip()
  25. if line[-1]>='0' and line[-1] <='9':
  26. dic[line[-1]].append(line)
  27. print(dic)
  28. #004
  29. #005
  30. fline=lines[0]
  31. fline=fline.split(',')
  32. for i in range(len(fline)):
  33. if fline[i] == "fixed acidity":
  34. pk=i
  35. break
  36. for i in quality:
  37. fa[i]=0
  38. for i in range(1,len(lines)):
  39. line=lines[i].strip()
  40. line=line.split(',')
  41. fa[line[-1]]+=1
  42. for key in fa.keys():
  43. fa[key] = fa[key]/len(dic[key])
  44. print("The average of fixed aciditys are: ")
  45. for key in fa.keys():
  46. print(key," ", fa[key])
  47. f.close()
Download Printable view

URL of this paste

Embed with JavaScript

Embed with iframe

Raw text