简体   繁体   English

在python的表中使用for循环

[英]using a for-loop in a table in python

I am trying to make a table that sorts out high-grossing films and the most similar films to them. 我正在尝试制作一张桌子,以整理出票房最高的电影和与之最相似的电影。 All of the code is completed but I'm having trouble finishing the table. 所有代码都已完成,但是我在整理表时遇到了麻烦。

my code: 我的代码:

#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>"

the table I come up with is mostly correct but the "Most Similar" films in every row all correspond to the first year [2000]. 我提出的表格基本上是正确的,但每一行的“最相似”电影都对应第一年[2000]。 How can I alter my code so that the "Most Similar" films will correspond to the right data? 如何更改我的代码,以使“最相似”电影对应正确的数据?

Some advices: 一些建议:

  • Why do you use "actor1 actor2 actor3" rather than main_actors = [] ? 为什么使用“ actor1 actor2 actor3”而不是main_actors = [] You can store dictionnaries inside! 您可以在其中存储词典!
  • You can also use seq in place of for c in [2000, …. ] 您也可以for c in [2000, …. ]使用seq代替for c in [2000, …. ] for c in [2000, …. ]
  • Finally, you could use printf like formatting with your strings (at the end) and have: 最后,您可以使用printf之类的字符串格式化(最后),并具有:

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])

But I think the actual problem is html in your case: you must not use th in eatch row. 但是我认为实际的问题是html:在切入行中不能使用th。 The syntax is the following: 语法如下:

<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>

But I may have missed something on you script. 但是我可能已经错过了一些脚本。

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>"

You print the same variables max_list[0:4] every time you go through the loop. 每次循环时,您都打印相同的变量max_list [0:4]。 Since you don't change those variables of course you get the same output each time. 由于您当然不会更改这些变量,因此每次都会获得相同的输出。

You need to either move your logic to decide which is most similiar into the loop or create a new loop which stores the most similar in a list which can fetch inside your loop. 您需要移动逻辑来确定最相似的循环,或者创建一个新循环,将最相似的循环存储在列表中,该列表可以在循环内部获取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM