2011-10-09 3 views
0

고득점 영화와 가장 유사한 영화를 분류하는 테이블을 만들려고합니다. 모든 코드가 완료되었지만 테이블을 완성하는 데 문제가 있습니다.파이썬의 테이블에서 for-loop를 사용합니다.

내 코드 :

#import the data from the csv file and use DictRead to interpret the information. 

import csv 

csv_file = open("moviestats_med.csv") 
csv_data = csv.DictReader(csv_file) 


#create dictionary of films and their directors. 
direct = {} 
#create dictionary of films and their genres. 
genre = {} 
#create dictionary of films and each main actor. 
actor1 = {} 
actor2 = {} 
actor3 = {} 
#create dictionary of films and their worldwide gross. 
gross = {} 
#create dictionary of films and the year they came out. 
year = {} 

name = {} 
#iterate over the csv file to fill the dictionaries. 
for c in csv_data: 
    direct[c['name']] = c['director'] 
    genre[c['name']] = c['genre'] 
    actor1[c['name']] = c['actor1'] 
    actor2[c['name']] = c['actor2'] 
    actor3[c['name']] = c['actor3'] 
    gross[c['name']] = c['Worldwide Gross'] 
    year[c['name']] = c['date'] 
    name[c['name']] = c['name']  

#create a two-variable function to deterime the FavActor Similarity score: 
def FavActorFunction(film1,film2): 
    #set the result of the FavActor formula between two films to a default of 0. 
    FavActorScore = 0 
    #add 3 to the similarity score if the films have the same director. 
    if direct[film1] == direct[film2]: 
     FavActorScore += 3 
    #add 2 to the similarity score if the films are in the same genre. 
    if genre[film1] == genre[film2]: 
     FavActorScore += 2 
    #add 5 to the similarity score for each actor they have in common.      
    if actor1[film1] in (actor1[film2], actor2[film2], actor3[film2]): 
     FavActorScore += 5 
    if actor2[film1] in (actor1[film2], actor2[film2], actor3[film2]): 
     FavActorScore += 5 
    if actor3[film1] in (actor1[film2], actor2[film2], actor3[film2]): 
     FavActorScore += 5  
    #print the resulting score.      
    return FavActorScore 

#create a function to find the film with the greatest Worldwide Gross per year. 
def MaxGrossFinder(c): 
    #set the intial maximum gross to zero. 
    MaxGross = 0 
    #replace the MaxGross with any film in that year that has a greater gross. 
    for film in year:      
     f = int(gross[film])       
     if year[film] == c: 
      if f > MaxGross: 
       MaxGross = f 
       max = film 
    #print the year and the max value for that year.     
    return max 

#create a dictionary for the max grossing films of each year from 2000-2007. 
max_films = {}      
#create a list of years from 2000-2007.      
for c in ['2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007']: 
    max_films[c] = MaxGrossFinder(c) 



if 'a' == 'a': 
    max_list = [] 
    MaxSimilarity = 0 
    for d in year: 
     f = FavActorFunction(max_films[c], d)  
     if d != MaxGrossFinder(c): 
      if year[d] == c: 
       if f > MaxSimilarity: 
        MaxSimilarity = f 
        max = d 
    max_list.append(max) 

    MaxSimilarity2 = 0    
    for d in year: 
     g = FavActorFunction(max_films[c], d)  
     if d != MaxGrossFinder(c): 
      if d != max: 
       if year[d] == c: 
        if g > MaxSimilarity2: 
         MaxSimilarity2 = g 
         max2 = d 
    max_list.append(max2) 

    MaxSimilarity3 = 0    
    for d in year: 
     h = FavActorFunction(max_films[c], d)  
     if d != MaxGrossFinder(c): 
      if d != max and d != max2: 
       if year[d] == c: 
        if h > MaxSimilarity3: 
         MaxSimilarity3 = h 
         max3 = d 

    max_list.append(max3) 



    MaxSimilarity4 = 0    
    for d in year: 
     i = FavActorFunction(max_films[c], d)  
     if d != MaxGrossFinder(c): 
      if d != max and d != max2 and d != max3: 
       if year[d] == c: 
        if i > MaxSimilarity4: 
         MaxSimilarity4 = i 
         max4 = d 
    max_list.append(max4) 




print "Content-Type: text/html" 
print "" 
print "<html>" 
print "<body>" 
print "<table border=1>" 

print "<tr>" 
print "<th><font color=green>Year</font></th>" 
print "<th><font color=blue>Highest Grossing Film</font></th>" 
print "<th><font color=red>Most Similar</font></th>" 
print "<th><font color=red>2nd Most Similar</font></th>" 
print "<th><font color=red>3rd Most Similar</font></th>" 
print "<th><font color=red>4th Most Similar</font></th>" 
print "</tr>" 


for c in sorted(max_films): 
    print "<tr><th>" 
    print c 
    print "<td>" 
    print max_films[c] 
    print "</td><td>" 
    print max_list[0] 
    print "</td><td>" 
    print max_list[1] 
    print "</td><td>" 
    print max_list[2] 
    print "</td><td>" 
    print max_list[3] 
    print "</td></tr></th>" 

내가 가지고 올 테이블이 대부분 정확하지만 모든 행에서 "대부분의 유사"영화는 모두 첫 해 [2000]에 해당합니다. "가장 유사한"영화가 올바른 데이터와 일치하도록 코드를 어떻게 변경합니까?

답변

3

어떤 조언 : 당신은 왜 "actor1 actor2 actor3"오히려 main_actors = [] 이상을 사용합니까

  • ? 내부에 사전을 저장할 수 있습니다!
  • 당신은 또한 당신이 (말에) 당신의 문자열 포맷처럼의 printf를 사용할 수, 마지막으로 for c in [2000, …. ]
  • 대신에 seq을 사용할 수 있습니다 :

print "<tr><th>%s<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td></td></tr></th>" % (c, max_films[c], max_list[0], max_list[1], max_list[2], max_list[3])

하지만 실제 생각 문제는 귀하의 경우 html입니다 : 당신은 eatch 행에서 th를 사용하지 않아야합니다. 구문은 다음과 같습니다.

<table> 
    <tr><th>Colname1</th><th>Colname2</th><th>Colname3</th></tr> 
    <tr><td>value 11</td><td>value 12</td><td>value 13</td></tr> 
    <tr><td>value 21</td><td>value 22</td><td>value 23</td></tr> 
    <tr><td>value 31</td><td>value 32</td><td>value 33</td></tr> 
</table> 

하지만 스크립트에서 뭔가를 놓쳤을 수 있습니다.

1
for c in sorted(max_films): 
    print "<tr><th>" 
    print c 
    print "<td>" 
    print max_films[c] 
    print "</td><td>" 
    print max_list[0] 
    print "</td><td>" 
    print max_list[1] 
    print "</td><td>" 
    print max_list[2] 
    print "</td><td>" 
    print max_list[3] 
    print "</td></tr></th>" 

루프를 돌 때마다 동일한 변수 max_list [0 : 4]가 인쇄됩니다. 물론 변수를 변경하지 않으므로 매번 동일한 출력을 얻습니다.

로직을 이동시켜 루프와 가장 유사한 것을 결정하거나 루프 내에서 가져올 수있는 목록에 가장 유사한 것을 저장하는 새 루프를 생성해야합니다.