<s>Human: 为给定的电影列表根据年份和评分进行排序。
[{"title": "The Godfather", "year": 1972, "rating": 9.2},{"title": "Forrest Gump", "year": 1994, "rating": 8.8},{"title": "Inception", "year": 2010, "rating": 8.7},{"title": "The Matrix", "year": 1999, "rating": 8.7}]
</s><s>Assistant: 可以使用Python中的sorted函数和lambda表达式对电影列表进行排序。

以下是根据年份从旧到新排序的示例代码：

movies = [{"title": "The Godfather", "year": 1972, "rating": 9.2},
          {"title": "Forrest Gump", "year": 1994, "rating": 8.8},
          {"title": "Inception", "year": 2010, "rating": 8.7},
          {"title": "The Matrix", "year": 1999, "rating": 8.7}]

result = sorted(movies, key=lambda x: x['year'])
print(result)

输出结果为：

[{'title': 'The Godfather', 'year': 1972, 'rating': 9.2},
 {'title': 'Forrest Gump', 'year': 1994, 'rating': 8.8},
 {'title': 'The Matrix', 'year': 1999, 'rating': 8.7},
 {'title': 'Inception', 'year': 2010, 'rating': 8.7}]


以下是根据评分从高到低排序的示例代码：

result = sorted(movies, key=lambda x: x['rating'], reverse=True)
print(result)

输出结果为：

[{'title': 'The Godfather', 'year': 1972, 'rating': 9.2},
 {'title': 'Forrest Gump', 'year': 1994, 'rating': 8.8},
 {'title': 'Inception', 'year': 2010, 'rating': 8.7},
 {'title': 'The Matrix', 'year': 1999, 'rating': 8.7}]
</s>