def loadRatings(ratingsFile):
"""
载入得分
"""
if not isfile(ratingsFile):
print ("File %s does not exist." % ratingsFile)
sys.exit(1)
f = open(ratingsFile, 'r')
ratings = filter(lambda r: r[2] > 0, [parseRating(line)[1] for line in f])
f.close()
if not ratings:
print ("No ratings provided.")
sys.exit(1)
else:
return ratings
myRatings = loadRatings("./recommendation_system_codes/movielens/ml-1m/ratings.dat")
myRatings[1]
myRatings[1]
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-22-961b4f3a9b9d> in <module>() ----> 1 myRatings[1] TypeError: 'filter' object is not subscriptable
分析:
In Python 2.7 filter()
returned a list
, which has .pop()
function.
In Python 3.x filter()
returns a filter
iterable object which does not.
Before you can .pop()
from the filter
in Python 3 you need to convert it to a list. So add e.g.
myRatings = list(myRatings)
myRatings[1]
after the colors = filter(...)
line. In Python 2.7 this will have no effect, so your code will continue to work there. See this question for more information and these docs.