http://www.mamicode.com/info-detail-2399086.html
有一个列表,每个元素存放学生姓名、成绩,按学生成绩从优到差排序。
stu=['张三:20','李四:70','王五:88','李六:40','赵琦:55.5']
def sortscore(A): for j in range(len(A)): for i in range(len(A)-1): if A[i].split(':')[1] < A[i+1].split(':')[1]: A[i],A[i+1] = A[i+1],A[i] i=i+1 else: i=i+1 j=j+1 return Aprint(sortscore(stu))有一个列表,每个元素存放学生姓名、成绩,计算学生的总成绩
stu=['张三:20','李四:70','王五:88','李六:40','赵琦:55.5']
def total(A): sum=0.0 if len(A) == 0: return 0 elif len(A) == 1: return float(A.split(':')[1]) else: for i in range(len(A)): sum=sum+float(A[i].split(':')[1]) return sumprint(total(stu))