【问题标题】:How to find same values in dictionary and assign a new item with averaging of duplicate values?如何在字典中找到相同的值并分配一个具有重复值平均值的新项目?
【发布时间】:2021-05-28 15:05:47
【问题描述】:

我有一个包含电影详细信息的文本文件,结构如下;

ID Rating Title Genre
0 4.5 Amelie (Fabuleux destin d'Amélie Poulain, Le) (2001) Comedy|Romance|Thriller
1 3.0 Die Hard (1988) Action|Crime|Thriller
2 4.0 Star Wars: Episode VI - Return of the Jedi (1983) Action|Adventure|Sci-Fi
3 3.5 Fugitive, The (1993) Thriller
4 4.5 Fugitive, The (1993) Thriller
5 2.5 Amelie (Fabuleux destin d'Amélie Poulain, Le) (2001) Comedy|Romance|Thriller

我想通过对我找到的电影的平均评分找到相同的电影并将它们保存为新项目。

例如; 逃犯在名单上重复。平均评分:4.0

{'580': ['4.0', 'Jurassic Park (1993)', ['Action', 'Adventure', 'Sci-Fi', 'Thriller']], 
 '264': ['4.0', 'Mask, The (1994)', ['Action', 'Comedy', 'Crime', 'Fantasy', 'Thriller']], 
 '370': ['4.5', "Amelie) (2001)", ['Comedy', 'Romance', 'Thriller']], 
 '312': ['3.0', 'Die Hard (1988)', ['Action', 'Crime', 'Thriller']], 
 '166': ['3.5', 'Fugitive, The (1993)', ['Thriller']], 
 '195': ['4.5', 'Fugitive, The (1993)', ['Thriller']]}

我已将文件中的每一行保存为字典中的一个项目。我怎样才能找到并平均相同的项目?谢谢。

【问题讨论】:

标签: python dictionary


【解决方案1】:

使用 pandas 读取文件,然后按“标题”分组并计算“评分”列的平均值

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-21
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    相关资源
    最近更新 更多