使用2位数和单位数编制最大值

时间:2015-05-03 23:18:14

标签: python csv max

我目前正在开发一个代码,该代码在csv文件中得分最高。例如,如果得分为10和4,则最高得分应为4.但如果数字为10则不起作用。如果数字为5和7或任何1位数,则工作正常。

http://postimg.org/image/7a0pgl6x3/

这是我用来训练每个用户得分的最大值的代码。

high = max(row[2:4]) # finds the highest score from the row 2:4
high = str("0") + str(high) # adds a 0 in front of the high score so that it could be used for sorting
row.insert( 6, high) #insert the high value on row 6
write.writerows([row[:7]])

非常感谢任何帮助或建议。 感谢。

1 个答案:

答案 0 :(得分:0)

我要继续猜测你的意思是10比10和4比较高分,而不是。在这种情况下,您的问题几乎肯定是由使用字符串引起的:

In [12]: max("10","4")
Out[12]: '4'

In [13]: max(10,4)
Out[13]: 10

我从作家那里假设你正在使用csv库。那个库不会从字符串中进行任何转换,所以当你认为你正在处理数字时,你实际上是处理字符串,并且在比较字符串“4”和“10”时首先比较他们的第一个角色......

一个很好的修复方法是在读取csv文件时进行转换,但快速修复只是使用以下内容:

high = max(int(x) for x in row[2:4])