简体   繁体   English

operator.itemgetter() 在这段代码中是如何工作的?

[英]How does operator.itemgetter() work in this code?

csv1 = csv.reader(scoreboard, delimiter=",")
sort = sorted(csv1, key=operator.itemgetter(0), reverse = True) 

for x in sort[:6]:
    print(x)

I am hoping someone can explain to me what's happening in the above code.我希望有人可以向我解释上面代码中发生了什么。

On first line you're oreading a comma separated file (.csv).在第一行,您正在读取一个逗号分隔的文件 (.csv)。 It will probably be a table or something similar.它可能是一张桌子或类似的东西。

On the second line you're sorting it based on the first element in descending order (biggest values first).在第二行,您将根据第一个元素按降序对其进行排序(首先是最大值)。 That means that you are sorting the table considering the first column.这意味着您正在考虑第一列对表格进行排序。

On the for loop you print the first 6 elements of the sorted array在 for 循环中,您打印排序数组的前 6 个元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM