如何完成功能?

时间:2020-10-30 00:11:44

标签: python list function for-loop

我使用此功能来查找列表中非零评分的平均值。在我看来,average是一个未使用的变量。我知道我的功能缺少某些东西,但是我一直在努力寻找它。如果可以的话,我将不胜感激。

def average_rating(all_ratings):
    average=[]
    for j in range(30):
        ch=0
        count=0
        for i in range(5):
            if all_ratings[i][j] != 0:
                ch = ch+all_ratings[i][j]
                count=count+1

1 个答案:

答案 0 :(得分:0)

在我看来,average是一个未使用的变量。

那是因为您没有附加任何内容。只是一个空的list

您非常亲密!将这条线添加到外部循环的底部可存储列表average中的30部电影的平均评分。

average.append(ch/count)

这是一个基于您的代码的有效示例:

film_names = ["The Shawshank Redemption",
              "The Godfather",
              "The Godfather: Part II",
              "The Dark Knight",
              "12 Angry Men",
              "Schindler's List",
              "The Lord of the Rings: The Return of the King",
              "Pulp Fiction",
              "The Good, the Bad and the Ugly",
              "Fight Club",
              "Joker",
              "The Lord of the Rings: The Fellowship of the Ring",
              "Forrest Gump",
              "Inception",
              "Star Wars: Episode V - The Empire Strikes Back",
              "The Lord of the Rings: The Two Towers",
              "The Matrix",
              "One Flew Over the Cuckoo's Nest",
              "Goodfellas",
              "Seven Samurai",
              "Se7en",
              "City of God",
              "Life Is Beautiful",
              "The Silence of the Lambs",
              "Star Wars: Episode IV - A New Hope",
              "It's a Wonderful Life",
              "Saving Private Ryan",
              "Spirited Away",
              "The Green Mile",
              "Léon: The Professional",]

all_ratings = [[5, 5, 4, 4, 3, 1, 2, 3, 4, 4, 4, 3, 4, 0, 0, 0,
                1, 2, 3, 4, 4, 4, 1, 4, 0, 0, 0, 1, 2, 5],
               [5, 0, 1, 2, 3, 1, 2, 3, 4, 4, 4, 5, 4, 2, 1, 0,
                1, 2, 0, 5, 0, 4, 1, 4, 2, 0, 0, 1, 0, 5],
               [5, 2, 3, 4, 4, 0, 0, 0, 4, 5, 0, 3, 0, 0, 0, 3,
                4, 0, 1, 4, 4, 4, 0, 4, 0, 3, 0, 1, 2, 5],
               [5, 0, 4, 0, 0, 4, 2, 3, 0, 0, 4, 0, 3, 0, 1, 0,
                1, 2, 3, 0, 2, 0, 1, 0, 0, 0, 4, 0, 1, 5],
               [5, 4, 3, 2, 1, 1, 2, 3, 4, 3, 4, 3, 4, 0, 3, 0,
                1, 2, 4, 4, 4, 4, 1, 4, 0, 0, 0, 1, 2, 5], ]

def average_rating(all_ratings):
    average=[]
    for j in range(30):
        ch=0
        count=0
        for i in range(5):
            if all_ratings[i][j] != 0:
                ch = ch+all_ratings[i][j]
                count=count+1
        average.append(ch/count)
    
    return average

for film, rating in zip(film_names, average_rating(all_ratings)):
    print(f"{rating:0.2f} - {film}")

输出:

5.00 - The Shawshank Redemption
3.67 - The Godfather
3.00 - The Godfather: Part II
3.00 - The Dark Knight
2.75 - 12 Angry Men
1.75 - Schindler's List
2.00 - The Lord of the Rings: The Return of the King
3.00 - Pulp Fiction
4.00 - The Good, the Bad and the Ugly
4.00 - Fight Club
4.00 - Joker
3.50 - The Lord of the Rings: The Fellowship of the Ring
3.75 - Forrest Gump
2.00 - Inception
1.67 - Star Wars: Episode V - The Empire Strikes Back
3.00 - The Lord of the Rings: The Two Towers
1.60 - The Matrix
2.00 - One Flew Over the Cuckoo's Nest
2.75 - Goodfellas
4.25 - Seven Samurai
3.50 - Se7en
4.00 - City of God
1.00 - Life Is Beautiful
4.00 - The Silence of the Lambs
2.00 - Star Wars: Episode IV - A New Hope
3.00 - It's a Wonderful Life
4.00 - Saving Private Ryan
1.00 - Spirited Away
1.75 - The Green Mile
5.00 - Léon: The Professional

有关更多pythonic和功能解决方案,请尝试:

def average_rating(all_ratings):
    for ratings in zip(*all_ratings):
        ratings = tuple(filter(None, ratings))
        yield sum(ratings) / len(ratings)

请注意,这两种解决方案都无法解决所有5个人将影片定为0的情况,这会导致被0除的异常。

相关问题