简体   繁体   中英

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). 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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