Python - 获取具有其中一项的最高值的字典(json 列表)的索引?

Python - get the index of a dictionary (json list) with the highest value of one of the items?

我使用 TMDB api [tmdbsimple https://pypi.org/project/tmdbsimple/] 它为我提供了 json,我想在其中提取 'file_path' 的 'dict posters' 项目中有最多的 'vote_count',我该怎么做?

import tmdbsimple as tmdb
myKey = '****' #api key https://developers.themoviedb.org/3/getting-started/introduction
tmdb.API_KEY = myKey

tvg_tmdbid = '67158'
lst_posters_mylang = tmdb.TV(tvg_tmdbid).images(language='pt')['posters']
if lst_posters_mylang != []:
    item = ???
    posterpath = lst_posters_mylang[item]['file_path']

注意:修复了变量的打印 'lst_posters_mylang'

打印(lst_posters_mylang) return 这个json:

[
    {'aspect_ratio': 0.6666666666666666, 'file_path': '/3o7f2Xjwl5hcoiioR9eGdD9ezHt.jpg', 'height': 2160, 'iso_639_1': 'pt', 'vote_average': 5.312, 'vote_count': 1, 'width': 1440
    },
    {'aspect_ratio': 0.6666666666666666, 'file_path': '/cZy35eGPBcXOagv3ujmfGVm4U1C.jpg', 'height': 2160, 'iso_639_1': 'pt', 'vote_average': 0.0, 'vote_count': 0, 'width': 1440
    },
    {'aspect_ratio': 0.6666666666666666, 'file_path': '/46CmxozfvZSpeg4cEZ86Sm6QOXT.jpg', 'height': 1500, 'iso_639_1': 'pt', 'vote_average': 0.0, 'vote_count': 0, 'width': 1000
    },
    {'aspect_ratio': 0.6666666666666666, 'file_path': '/oWr83fLF26UUn6tPKNMk2U7fqLr.jpg', 'height': 750, 'iso_639_1': 'pt', 'vote_average': 0.0, 'vote_count': 0, 'width': 500
    },
    {'aspect_ratio': 0.66625, 'file_path': '/22YerMIPRTPe4jG1A2a9jgpBWxo.jpg', 'height': 800, 'iso_639_1': 'pt', 'vote_average': 0.0, 'vote_count': 0, 'width': 533
    }
]

在这种情况下,'posters' 的项目 0 具有最高的 'vote_count' = 1,如何通过 python 获取此“海报”项目值? 只有这样我才能得到 'file_path' 值:

item = 0

海报路径 = lstposters_mylang[项目]['file_path']

posterpath ---- '/3o7f2Xjwl5hcoiioR9eGdD9ezHt.jpg'

使用 pythons 内置 sorted,您可以通过 key 参数定义自己的排序标准。

默认情况下它会升序排序,因此您需要设置 reverse=True

然后你可以从列表中获取第一项以获得最大值。

best_poster = sorted(lst_posters_mylang['posters'], key=lambda bd:bd["vote_count"], reverse=True)[0]

旁注:空数组是错误的,所以你可以做 if lst_posters_mylang:

我更改了您的投票计数,以便您可以看到它会排序并找到正确的值。

data = [
    {'aspect_ratio': 0.6666666666666666, 'file_path': '/3o7f2Xjwl5hcoiioR9eGdD9ezHt.jpg', 'height': 2160, 'iso_639_1': 'pt', 'vote_average': 5.312, 'vote_count': 1, 'width': 1440
    },
    {'aspect_ratio': 0.6666666666666666, 'file_path': '/cZy35eGPBcXOagv3ujmfGVm4U1C.jpg', 'height': 2160, 'iso_639_1': 'pt', 'vote_average': 0.0, 'vote_count': 5, 'width': 1440
    },
    {'aspect_ratio': 0.6666666666666666, 'file_path': '/46CmxozfvZSpeg4cEZ86Sm6QOXT.jpg', 'height': 1500, 'iso_639_1': 'pt', 'vote_average': 0.0, 'vote_count': 6, 'width': 1000
    },
    {'aspect_ratio': 0.6666666666666666, 'file_path': '/oWr83fLF26UUn6tPKNMk2U7fqLr.jpg', 'height': 750, 'iso_639_1': 'pt', 'vote_average': 0.0, 'vote_count': 3, 'width': 500
    },
    {'aspect_ratio': 0.66625, 'file_path': '/22YerMIPRTPe4jG1A2a9jgpBWxo.jpg', 'height': 800, 'iso_639_1': 'pt', 'vote_average': 0.0, 'vote_count': 2, 'width': 533
    }
]
data_tuples = [] # iterate through items and collect tuples of file_path and vote_count
for d in data:
    data_tuples.append((d['file_path'], d['vote_count']))
file_path_max_votes = sorted(data_tuples, key = lambda x: x[1])[-1][0]
# this sorts tuples ascending, [-1] chooses the last tuple in the list; [0] takes the first value of the tuple

输出

In [164]: file_path_max_votes
Out[164]: '/46CmxozfvZSpeg4cEZ86Sm6QOXT.jpg'