您的位置:首页 > 编程语言 > Python开发

使用Python找出历史上5星好评数最多的50部电影,基于MovieLens20M数据集

2016-01-18 20:15 633 查看
第1名:"Shawshank Redemption The (1994)"  类型:Crime|Drama  5星好评次数:31896
第2名:Pulp Fiction (1994)  类型:Comedy|Crime|Drama|Thriller  5星好评次数:27762
第3名:"Silence of the Lambs The (1991)"  类型:Crime|Horror|Thriller  5星好评次数:22513
第4名:Schindler's List (1993)  类型:Drama|War  5星好评次数:22355
第5名:Star Wars: Episode IV - A New Hope (1977)  类型:Action|Adventure|Sci-Fi  5星好评次数:22117
第6名:Forrest Gump (1994)  类型:Comedy|Drama|Romance|War  5星好评次数:21292
第7名:"Godfather The (1972)"  类型:Crime|Drama  5星好评次数:20251
第8名:"Usual Suspects The (1995)"  类型:Crime|Mystery|Thriller  5星好评次数:19914
第9名:"Matrix The (1999)"  类型:Action|Sci-Fi|Thriller  5星好评次数:18582
第10名:Braveheart (1995)  类型:Action|Drama|War  5星好评次数:18467
第11名:Star Wars: Episode V - The Empire Strikes Back (1980)  类型:Action|Adventure|Sci-Fi  5星好评次数:16987
第12名:American Beauty (1999)  类型:Comedy|Drama  5星好评次数:15719
第13名:Raiders of the Lost Ark (Indiana Jones and the Raiders of the Lost Ark) (1981)  类型:Action|Adventure  5星好评次数:15365
第14名:Fargo (1996)  类型:Comedy|Crime|Drama|Thriller  5星好评次数:15232
第15名:Fight Club (1999)  类型:Action|Crime|Drama|Thriller  5星好评次数:14623
第16名:Star Wars: Episode VI - Return of the Jedi (1983)  类型:Action|Adventure|Sci-Fi  5星好评次数:13409
第17名:Terminator 2: Judgment Day (1991)  类型:Action|Sci-Fi  5星好评次数:13317
第18名:"Lord of the Rings: The Fellowship of the Ring The (2001)"  类型:Adventure|Fantasy  5星好评次数:12623
第19名:Toy Story (1995)  类型:Adventure|Animation|Children|Comedy|Fantasy  5星好评次数:12268
第20名:"Fugitive The (1993)"  类型:Thriller  5星好评次数:12127
第21名:"Princess Bride The (1987)"  类型:Action|Adventure|Comedy|Fantasy|Romance  5星好评次数:12086
第22名:Seven (a.k.a. Se7en) (1995)  类型:Mystery|Thriller  5星好评次数:12021
第23名:"Godfather: Part II The (1974)"  类型:Crime|Drama  5星好评次数:11737
第24名:Monty Python and the Holy Grail (1975)  类型:Adventure|Comedy|Fantasy  5星好评次数:11512
第25名:Saving Private Ryan (1998)  类型:Action|Drama|War  5星好评次数:11274
第26名:Blade Runner (1982)  类型:Action|Sci-Fi|Thriller  5星好评次数:10787
第27名:One Flew Over the Cuckoo's Nest (1975)  类型:Drama  5星好评次数:10678
第28名:Apollo 13 (1995)  类型:Adventure|Drama|IMAX  5星好评次数:10546
第29名:Casablanca (1942)  类型:Drama|Romance  5星好评次数:10466
第30名:"Sixth Sense The (1999)"  类型:Drama|Horror|Mystery  5星好评次数:10329
第31名:"Lord of the Rings: The Two Towers The (2002)"  类型:Adventure|Fantasy  5星好评次数:10241
第32名:Jurassic Park (1993)  类型:Action|Adventure|Sci-Fi|Thriller  5星好评次数:10240
第33名:"Lord of the Rings: The Return of the King The (2003)"  类型:Action|Adventure|Drama|Fantasy  5星好评次数:10107
第34名:Twelve Monkeys (a.k.a. 12 Monkeys) (1995)  类型:Mystery|Sci-Fi|Thriller  5星好评次数:9628
第35名:Dr. Strangelove or: How I Learned to Stop Worrying and Love the Bomb (1964)  类型:Comedy|War  5星好评次数:9546
第36名:Dances with Wolves (1990)  类型:Adventure|Drama|Western  5星好评次数:9167
第37名:Back to the Future (1985)  类型:Adventure|Comedy|Sci-Fi  5星好评次数:8963
第38名:Alien (1979)  类型:Horror|Sci-Fi  5星好评次数:8754
第39名:Goodfellas (1990)  类型:Crime|Drama  5星好评次数:8656
第40名:Memento (2000)  类型:Mystery|Thriller  5星好评次数:8490
第41名:2001: A Space Odyssey (1968)  类型:Adventure|Drama|Sci-Fi  5星好评次数:8272
第42名:"Lion King The (1994)"  类型:Adventure|Animation|Children|Drama|Musical|IMAX  5星好评次数:8104
第43名:Taxi Driver (1976)  类型:Crime|Drama|Thriller  5星好评次数:7915
第44名:"Amelie (Fabuleux destin d'Amélie Poulain Le) (2001)"  类型:Comedy|Romance  5星好评次数:7898
第45名:L.A. Confidential (1997)  类型:Crime|Film-Noir|Mystery|Thriller  5星好评次数:7744
第46名:Gladiator (2000)  类型:Action|Adventure|Drama  5星好评次数:7742
第47名:Reservoir Dogs (1992)  类型:Crime|Mystery|Thriller  5星好评次数:7619
第48名:Apocalypse Now (1979)  类型:Action|Drama|War  5星好评次数:7531
第49名:Babe (1995)  类型:Children|Drama  5星好评次数:7480
第50名:Aliens (1986)  类型:Action|Adventure|Horror|Sci-Fi  5星好评次数:7419

Python3.5
# -*- coding:utf8 -*-
fileRatings=r"ratings.csv"
fileMovies=r"movies.csv"
N=50
movieDict={}
#读取topN到字典
ratingFile=open(fileRatings)
for line in ratingFile:
#ratings.csv评分表格文件里边存储的数据格式
#userId,movieId,rating,timestamp
#1,2,3.5,1112486027
#1,29,3.5,1112484676
movieId=line.strip().split(',')[1]
movieScore=line.split(',')[2]
#   因为存储的是字符串,所以要和字符串比较
if(movieScore=='5.0'):
if(movieId in movieDict):
movieDict[movieId]=movieDict[movieId]+1
else:
movieDict[movieId]=1
ratingFile.close()
##按照5星好评人数倒排
topN=sorted(movieDict.items(),key=lambda asd:asd[1],reverse=True)[0:N]
print(topN)
#根据id获取电影名称
ranking=0;
result=[]
for eachId in topN:
ranking=ranking+1
movieFile=open(fileMovies,'r',encoding='UTF-8')
for line in movieFile:
#movie.csv电影表格文件存储的数据格式
#movieId,title,genres
#1,Toy Story (1995),Adventure|Animation|Children|Comedy|Fantasy
#2,Jumanji (1995),Adventure|Children|Fantasy
#3,Grumpier Old Men (1995),Comedy|Romance
movieId=line.strip().split(',')[0]
if(movieId==eachId[0]):
if(len(line.strip().split(','))==4):
movieName=line.strip().split(',')[1]+line.strip().split(',')[2]
else:
movieName=line.strip().split(',')[1]
movieType=line.strip().split(',')[-1]
#            content='第%s名:'%(ranking),movieName,'类型:',movieType,'5星好评次数:',eachId[1]
content='第%s名:'%(ranking)+str(movieName)+'  类型:'+movieType+'  5星好评次数:'+str(eachId[1])
#            print(content)
result.append(content)
movieFile.close()
break
else:
pass
movieFile.close()

#for each in result:
#    print(each)
f=open('5星好评数最多的50部电影.txt', 'w')
for i in result:
print(i)
f.write(i)
f.write("\n")
f.close()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: