【问题标题】:Calculating the max value from a set of data从一组数据中计算最大值
【发布时间】:2015-05-04 17:05:22
【问题描述】:

我目前正在开发一个代码,它可以根据用户的选择对 csv 文件中的数据进行排序。

目前我遇到了麻烦,因为我的最高分排序并没有真正起作用。它将 10 识别为 010,因此推断它小于 09 等其他数字。有什么办法可以解决这个问题吗?

http://postimg.org/image/onfbhpaxn/这是我目前得到的结果

这是我用来锻炼最高分的代码。

high = max(int(x) for x in 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]])

这是我用来排序的代码。

 if choice == 2: #if choice is 2
     form = input("Enter a class to sort it's Data: ") 
     filename = 'class{}.csv'.format(form)
     print("• Enter 1 to sort by student's highest score in alphabetical order. ")
     print("• Enter 2 to sort by student's highest score (highest to lowest)")
     print("• Enter 3 to sort by Student's average score (highest to lowest)")
     sorttype = int(input("Choose a option from above to sort: "))  # ask the teacher to choose an option for the sorting from above
     print("                                                                                       ")  #prints out nothing (space ) so that the sorted data is layed out in a neater way 
     with open (filename,'r', newline='') as keerthan: #open the file to read using csv writer
         read = csv.reader(keerthan,delimiter=',') #creates a function to read lines in the sort file
         if sorttype == 1: sorting = sorted(read, key=operator.itemgetter(0,6)) # if the student choses 1 ; sort it in alphabetical order
         elif sorttype == 2:sorting = sorted(read, key=operator.itemgetter(6,0), reverse = True) #if choice 2 , sort it is lowest to highest and then reverse.
         elif sorttype == 3:sorting = sorted(read, key=operator.itemgetter(5,0), reverse = True) # if choice 3 , sort by average score ; lowest to highest and then reverse.
         for row in sorting: # for every row in the file
             if row[0].startswith("Name"):None #if the first column is "name" (like the header): skip it

             else: #else (otherwise)
                 if sorttype == 3: print(row[0] + ' ' + row[1] + ': ' + row[5]) #otherwise print FirstName, SecondName and average score

                 else:print(row[0] + ' ' + row[1] + ': ' + row[6])

谢谢。

【问题讨论】:

  • 你应该用你正在使用的语言标记它(python,我假设)
  • @arvi1000 啊,我的错,谢谢

标签: python sorting csv max highest


【解决方案1】:

由于您要做的是根据数字进行排序,因此您应该让key 函数将您想要的值转换为整数。下面是一个帮助您入门的示例:

values = [('Alice', '80'), ('Bob', '5'), ('Chuck', '80')]
sorted(values, key = lambda row: (-int(row[1]), row[0]))

【讨论】:

  • 感谢您的帮助,所以如果 sorttype == 1:sorting = sorted(read, key=operator.itemgetter(0,6)) 我应该使用你提供的东西而不是这个?还是您建议我在排序之前将值更改为整数?
  • 第一个排序按照您的预期工作,对吗?我建议将第二个和第三个排序更改为基于整数值而不是字符串进行排序。
  • 是的。啊,我明白了,我对 lambda 不是很熟悉:/ 但我会尝试看看会发生什么。谢谢
  • 你好,所以我做了这个 elif sorttype == 2:sorting = sorted(read, key = lambda row: -int(row[1], reverse = True)) 而不是我原来的排序最高分。但是我得到了一个错误。 postimg.org/image/6dm1a23yz 另外,我的排序对吗?还是我做了一些愚蠢的事情? postimg.org/image/w4gw82n45
  • 所以lambda只是一种快速定义函数的方式,而key参数需要一个函数。您想要的是一个返回您实际想要排序的值的函数。当您只想对值进行简单排序时,operator.itemgetter 是一个不错的选择。尝试编写一个函数,获取一行数据并返回要排序的值!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-27
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2020-12-23
  • 1970-01-01
相关资源
最近更新 更多