淘先锋技术网

首页 1 2 3 4 5 6 7

写这么丑的代码我也是醉了,继续学习。
第二周的assignment:

  1. Mean Rating: Calculate the mean rating for each movie, order with the highest rating listed first, and submit the top 5.
  2. % of ratings 4+: Calculate the percentage of ratings for each movie that are 4 or higher. Order with the highest percentage first, and submit the top 5.
  3. Rating Count: Count the number of ratings for each movie, order with the most number of ratings first, and submit the top 5.
  4. Top 5 Star Wars: Calculate movies that most often occur with Star Wars: Episode IV - A New Hope (1977) using the (x+y)/x method described in class. In other words, for each movie, calculate the percentage of Star Wars raters who also rated that movie. Order with the highest percentage first, and submit the top 5.
#coding:utf-8
import csv
#top n function
def topn(name,scores,n=):
    tmpscores=scores[:] #create a new array
    tmpscores.sort()
    flag=[ for i in range(len(name))] # flags 
    for i in range(n):
        for j in range(len(name)):
            if scores[j]==tmpscores[--i]:
                if flag[j]:
                    flag[j]=
                    print name[j],scores[j]
def caldiv(name,array1,array2):
    result=[ for i in range(len(name))]
    for i in range(len(name)):
        if i!=:
            result[i]=array1[i]*/array2[i]
    return result
star_level=
csvfile=file('A1Ratings.csv','rU')
reader=csv.reader(csvfile,dialect='excel')
for line in reader:
    if reader.line_num==:
        name=line
        scores=[ for i in range(len(name))]
        totalcount=[ for i in range(len(name))]
        star_count=[ for i in range(len(name))]
    if reader.line_num!=:
        for num in name:
            ff=name.index(num)
            if ff>:
                temp=
                item=line[ff]
                if not item.strip(): # to solve the proble of ""
                    item=
                    temp=
                scores[ff]=scores[ff]+int(item)
                totalcount[ff]=totalcount[ff]+temp
                if int(item)>=star_level:
                    star_count[ff]=star_count[ff]+
average=caldiv(name,scores,totalcount)
average1=caldiv(name,star_count,totalcount)
topn(name,average)
topn(name,average1)
csvfile.close()
csvfile=file('A1Ratings.csv','rU')
reader=csv.reader(csvfile,dialect='excel')
sit=
count=[ for i in range(len(name))]
for line in reader:
    if reader.line_num!=:
        for i in range(len(name)):
            if not line[i].strip():
                line[i]=
            if i> and i!=sit:
                if  int(line[sit])*int(line[i]):
                    count[i]=count[i]+/
topn(name,count,)

中间遇到的问题:
1. new-line character seen in unquoted field - do you need to open the file in universal-newline mode?
initial code:
csvfile1=file(‘A1Ratings.csv’,’rb’)
update code:
csvfile1=file(‘A1Ratings.csv’,’rU’)
2. 问题代码如下:

for line in reader:
    print line
    if reader.line_num==:
        name=line
        scores=[ for i in range(len(name))]
        totalcount=[ for i in range(len(name))]
    if reader.line_num!=:
        for num in name:
            if name.index(num)>:
                for item in line:
                    if name.index(num)==line.index(item):
                        temp=
#       print item,num
                        if not item.strip():                
                            item=
                            temp=
                        scores[name.index(num)]=scores[name.index(num)]+int(item)
                        totalcount[name.index(num)]=totalcount[name.index(num)]+temp
print scores
print totalcount
csvfile.close()

打flag问题,如果没有打flag,当有相同的分数时(如4分),会定位到第一个打4分的位置。