Python Floats / Sum函数

时间:2015-11-06 16:52:29

标签: python

一个。阅读文件并报告每个学生的成绩。成绩是每个问题的成绩总和。列是制表符分隔的。您需要使用float()函数将字符串转换为float。您还可以使用sum()函数,该函数将列表作为输入并返回其元素的总和。

原始文件:

%%file midterm-grades.txt
1.1 1.2 1.3 1.4 1.5 2.1 2.2 2.3 3.1 3.2 3.3 3.4 3.5 3.6 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 5.1 5.2 5.3 5.4a    5.4b    5.5 5.6
===========================================================================================================================
1   1   1   1   1   3   3   4   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   2   2   2   2   2
===========================================================================================================================
0   0.5 0.5 1   1   3   2   3   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   1.5 2   0   1.5 1.5 1.5
0   0.5 1   1   1   3   3   4.5 1.5 1.5 1.5 1.5 0.5 1.5 0   0   0   0   0   0   0   0   2   2   1.5 1.5 2   2   2
0   0.7 0.5 1   1   3   0   0   1.5 1.5 1   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 2   2   2   2   1   2   2
1   0.5 0.8 1   1   3   0   0   1.5 1.5 1.3 1.5 1.5 0.7 1.5 1.5 1.5 1.5 0   1.5 1.5 1.5 2   1.5 2   0   2   2   2
1   1   1   1   1   1.5 2   2   1.5 1.5 1   1.5 1.5 0.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   1.5 1.5 2   2   1
1   0.7 0.5 0   0   2.5 0   0   1.5 1.5 1.5 1.5 0   0.5 1.5 1.5 1.5 0.5 0.5 0   0.5 0.5 2   2   2   0   0   0   0
1   1   1   1   1   3   0   0   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 0.5 1.5 2   2   2   0   2   1.5 0
1   0.7 0.8 1   1   3   3   3   1.5 1.5 1   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 2   2   1.5 1.5 0   0   0
1   1   1   1   1   3   3   4   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 2   2   1.5 1.5 0   1   0
1   1   1   1   1   2.5 3   4   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 0   1.5 1.5 1.5 2   2   2   2   2   2   2
1   1   1   1   1   2   3   4.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 0.5 1.5 1.5 0.5 1.5 2   2   1.5 2   2   2   2
1   0.5 1   1   1   3   2.5 4   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   1.5 1.5 2   2   2
1   0.7 0.8 1   1   1.5 3   5   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   2   1.5 1   2   2
1   1   1   1   1   3   3   3   1.5 1.5 1   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   1.5 0   1   0   0
1   1   1   1   1   3   3   5   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   2   2   2   2   2
1   0.7 0.5 1   1   1.5 1   0.5 1.5 1.5 1.5 1.5 1.5 0   1.5 1.5 1.5 1.5 1.5 1.5 0.5 1.5 2   2   1.5 2   0   0   0
1   0.7 0.8 1   1   0   1   0.5 0   1.5 1   1.5 0   0.5 1.5 1.5 1.5 1.5 1.5 0   0.5 1.5 2   2   1.5 0   0   0   0
1   0.7 0.8 1   1   1.5 0   0.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 2   2   2   0   2   2   2
1   0.7 0.8 0.5 1   3   3   4   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   2   0   2   2   1.5
1   1   1   1   1   3   3   5   1.5 1.5 1.5 1.5 1.5 0.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 2   2   2   2   2   2   2
1   1   1   1   1   3   3   5   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 0.5 1.5 1.5 1.5 2   2   1.5 1.5 2   2   2
1   1   1   1   1   3   3   5   1.5 1.5 1.5 1.5 1.5 0.5 1.5 1.5 0.5 1.5 1.5 1.5 1.5 1.5 2   2   2   2   1   1   2
1   1   1   1   1   3   2   3   1.5 1.5 1   1.5 1.5 1   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   2   2   1.5 0   2   0   2
1   1   1   1   1   3   3   5   1.5 1.5 1   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.3 1.5 2   2   2   2   2   1   2
1   1   1   0.5 0.5 2.5 0   0   1.5 1.5 1.5 1.3 0   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   1.5 0   1   2   0
1   1   1   1   1   3   0   0   1.5 1.5 1   1.5 0   1.5 1.5 1.5 1.5 0.5 1.5 1.5 1.5 1.5 2   2   1.5 0   2   2   0
1   0.5 0.8 1   1   3   0   0   1.5 1.5 1   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 1   1.5 2   2   2   0   2   2   2
1   1   1   1   1   3   3   5   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 1   1.5 2   2   2   0   1   2   2
1   0.7 1   1   1   3   2   4   1.5 1.5 1   1.5 1.5 1   1.5 1.5 1.5 1.5 1   1.5 0.5 1.5 2   2   2   0   2   2   2
1   0.5 1   1   1   3   2   3   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   1.5 1   2   2   2
1   0.7 1   1   1   2.5 3   3   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 0.5 1.5 2   2   1.5 0   2   2   1.5
1   0.7 0.8 1   1   3   2   2   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 1.5 1.5 2   2   1   0   2   2   2
1   1   1   1   1   3   3   4   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   1.5 1.5 2   2   2
1   0.5 0.3 1   1   1.5 3   3.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   1.5 0   1   1   1
1   0.8 1   0   0   0   0   0   1.5 1.5 1   1.5 1.5 0   1.5 1.5 1.5 1.5 1.5 1.5 0.5 1.5 2   2   1.5 1.5 2   2   1.5
1   0.5 0.8 1   1   3   1   1.5 1.5 1.5 1   1.5 1.5 1.5 1.5 1.5 1.5 0.5 1.5 1.5 1.5 1.5 2   2   2   2   2   2   2
1   0.3 0.8 1   1   1.5 3   3   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1   1.5 1   1.5 2   2   1.5 1.5 2   2   2
1   1   1   1   1   3   2   2   1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 1.5 2   2   2   2   2   2   2

这是我的代码:

# Read file
midterm = open("midterm-grades.txt", "r")
# Report grade for each student
midterm_content = midterm.read()
indiv = midterm_content.split("\n")
# Get rid of Headers
students = indiv[4:]
for i in students : 
    withouttabs = i.split("\t")
    print "Student grade is ", sum(float(withouttabs))

无法弄清楚如何浮动等级以便我可以执行求和功能,或者此时几乎完全丢失。

1 个答案:

答案 0 :(得分:0)

如上所述,你不能在字符串列表上浮点()。只需迭代单个字符串列表,并一次一个地浮动(),如下所示:

midterm = open("midterm-grades.txt", "r")
# Report grade for each student
midterm_content = midterm.read()
indiv = midterm_content.split("\n")
# Get rid of Headers
students = indiv[4:]
for i in students : 
    withouttabs = i.split("\t")
    total = 0   ## initialize total for each student
    for j in withouttabs:   ## now, iterate over each list of strings
        total = total + float(j)
    print "Student grade is ", total   ## finally, print the total