干得好:

class Student(object):

def __init__(self, name):

self.name = name

self.scores = []

self.highestscore = 0

def __repr__(self):

return '<' + self.name + ', ' \

+ 'high=' + str(self.highestscore) + ', ' \

+ 'avg=' + str(self.getAverage()) + '>'

def addScore(self, score):

self.scores.append(score)

self.highestscore = max(self.highestscore, score)

def getAverage(self):

l = len(self.scores)

if l == 0:

return 0

else:

return sum(self.scores) / l

import csv

results = open("C:\Projetos\Data\Python\Scores1.csv", "r+")

csv1 = csv.reader(results, delimiter=",")

data = {}

highscore = 0

for eachline in csv1:

#print(eachline)

name = eachline[0]

student = data.get(name, None)

if student == None:

student = Student(name)

data[name] = student

score = int(eachline[1])

student.addScore(score)

students = [data[key] for key in data]

sorted_alphabetically = sorted(students, key=lambda x:x.name)

sorted_by_highest_score = sorted(students, key=lambda x:x.highestscore, reverse=True)

sorted_by_avg_score = sorted(students, key=lambda x:x.getAverage(), reverse=True)

print 'sorted_alphabetically =', sorted_alphabetically

print 'sorted_by_highest_score =', sorted_by_highest_score

print 'sorted_by_avg_score =', sorted_by_avg_score

输入:

a,3

b,4

c,5

e,4

a,2

b,5

c,6

e,9

a,1

b,3

c,2

e,4

输出:

sorted_alphabetically = [, , , ]

sorted_by_highest_score = [, , , ]

sorted_by_avg_score = [, , , ]

python打开csv文件、计算总成绩_python – 读取CSV文件,计算平均值并打印所述平均值...相关推荐

  1. python打开csv文件设定分隔符_Python读取csv文件分隔符设置方法

    Windows下的分隔符默认的是逗号,而MAC的分隔符是分号.拿到一份用分号分割的CSV文件,在Win下是无法正确读取的,因为CSV模块默认调用的是Excel的规则. 所以我们在读取文件的时候需要添加 ...

  2. python读取csv文件路径设置_python读取csv文件

    1 importcsv2 tsvfile = open('any_different_file/excel2.tsv','rt')3 readers =csv.reader(tsvfile)4 for ...

  3. 如何用python处理excel中时间重合_python读取Excel文件中的时间,并计算时间间隔...

    以此Excel文件为例: 1.python读取Excel文件中的时间 import xlrd #读取Excel文件 workbook = xlrd.open_workbook('C:/Users/12 ...

  4. python csv数据处理生成图_Python 读取CSV文件数据并生成可视化图形

    CSV 文件数据读取并生成图形 CSV 文件格式 在文本文件中存储数据,最简单的方式就是将数据按照一定的格式存放在文件中,在读取文件时根据设计好的格式去将对应数据读取出来,这样就能很方便的进行简单数据 ...

  5. python 读取csv某一列_Python 读取csv的某列

    CSV 本质上是文本文件,能提供与 Excel 的表格类似的二维数据结构.因为是文本文件,读写都比 Excel 方便容易的多,经常拿来作为数据的中转格式.本文讨论如何快速有效的按列读取 CSV 文件. ...

  6. python读取大文件的坑_Python读取大文件的坑“与内存占用检测

    python读写文件的api都很简单,一不留神就容易踩"坑".笔者记录一次踩坑历程,并且给了一些总结,希望到大家在使用python的过程之中,能够避免一些可能产生隐患的代码. 1. ...

  7. python打开mat图像文件格式_如何读取mat文件 python

    展开全部 一.mat文件 mat数据格32313133353236313431303231363533e58685e5aeb931333363393664式是Matlab的数据存储的标准格式.在Mat ...

  8. python取excel单元格数值_python读取excel文件单元格中的数据返回类型

    python读取excel表格内不同类型的数据 不同类型数据对应值: #coding=utf-8 import os import xlrd from datetime import datetime ...

  9. python读取大文件太慢_python读取大文件越来越慢的原因与解决

    背景: 今天同事写代码,用python读取一个四五百兆的文件,然后做一串逻辑上很直观的处理.结果处理了一天还没有出来结果.问题出在哪里呢? 解决: 1. 同事打印了在不同时间点的时间,在需要的地方插入 ...

  10. python读取大文件的坑_python读取大文件踩过的坑——读取txt文件词向量

    在读取https://github.com/Embedding/Chinese-Word-Vectors中的中文词向量时,选择了一个有3G多的txt文件,之前在做词向量时用的是word2vec,所以直 ...

最新文章

  1. 74芯片引脚真值表汇总
  2. mongodb 事务_MongoDB 事务 — 基础入门篇
  3. 大型网站系统架构系列:负载均衡详解(一)
  4. vue父子组件、兄弟组件之间的通信和访问
  5. OpenCASCADE绘制测试线束:拓扑命令之扫掠Sweeping
  6. des算法密码多长_密码学中的多个DES
  7. 大三软件工程小项目-小技术集合-tcp服务器搭建及客户端
  8. mysql推荐内存_MySQL大内存配置方案 如my-medium.ini、my-huge.ini等
  9. 解决:浏览器无法及时更新css样式等改动
  10. 创建一个WPF+EF应用程序
  11. P1491 集合位置
  12. 爬取搜狗词库测试可行
  13. 实现html文件和c# 交互
  14. HEVC与AVC的区别与联系(十二),Android面试题集锦在这里
  15. 西门子SITOP电源维修6EP1336-3BA00冗余电源模块维修概述
  16. com4j学习(2):Visio自定义模具和形状,并添加连接点
  17. Reactor(反应器)模式
  18. 李沐论文精读系列四:CLIP和改进工作串讲(LSeg、GroupViT、VLiD、 GLIPv1、 GLIPv2、CLIPasso)
  19. [QT]clicked(bool)与toggled(bool)区别
  20. Android 实现点击输入框以外的区域隐藏软键盘

热门文章

  1. python md5解密方法与技巧_python ---- 爬取 md5解密结果 的小脚本
  2. SpringBoot+PageHelper实现分页功能
  3. 售前技能——自我定位
  4. mysql中ibd文件是很么_ibd文件怎么打开
  5. 用FFmpeg保存JPEG图片
  6. redis 集群介绍及docker实现
  7. OJ 2513: 小勇学分数
  8. 假设检验的基本思想及推理方法
  9. 123457123457#0#-----com.yuming.ZuiNiuChengYu--前拼后广--最牛成语
  10. 新一批交通强国试点工作启动