我正在使用heapq从列表列表中获取最大的元素.我写的程序如下.
import csv import heapq f = open("E:/output.csv","r") read = csv.reader(f) allrows = [row for row in read] for i in xrange(0,2): print allrows[i] allrows.sort(key=lambda x: x[2]) #this is working properly it=heapq.nlargest(20,enumerate(allrows),key=lambda x:x[2]) #error
我只想要前20名的元素.因此,我没有考虑使用堆.我得到的错误是,
Traceback (most recent call last): File "D:\eclipse_progs\DaDv\IMDB\Assignment1.py", line 42, init=heapq.nlargest(2,enumerate(allrows),key=lambda x:x[2]) File "C:\Python27\lib\heapq.py", line 470, in nlargest result = _nlargest(n, it) File "D:\eclipse_progs\DaDv\IMDB\Assignment1.py", line 42, in it=heapq.nlargest(2,enumerate(allrows),key=lambda x:x[2]) IndexError: tuple index out of range
我能知道为什么我会收到错误以及如何解决它.有没有使用heapq的属性我缺少.