开发者

creating a list with separating by character in Python

I'm trying to make a list of the top 4 maximum values with data from other dictionaries, but the result i get keeps separating each term into individual characters. The full code is:

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_li开发者_StackOverflowst.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 max_list

the result I get is ['3', '0', '0', 'T', 'r', 'a', 'n', 's', 'f', 'o', 'r', 'm', 'e', 'r', 's', 'S', 'p', 'i', 'd', 'e', 'r', '-', 'M', 'a', 'n', ' ', '3', '3', '0', '0']

how do I alter this code to get ['300', Transformers, etc.]


It appears that your function FavActorFunction is returning a list of characters rather than a list of words.

If you iterate over a string, rather than iterating over a list of strings, this can happen. For example, notice what happens when I convert a string to a list:

>>> print list("Hello, world")
print list("Hello, world")
['H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd']

And since you are just now learning, consider this advice: use descriptive variable names. Names like d, c, h and g make your code almost impossible to understand. Use variables like actor, director, etc. You don't win bonus points for being concise (unless you have a bad instructor).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜