gpt4 book ai didi

python - 你如何调用 json 字典中列表中的值

转载 作者:行者123 更新时间:2023-11-28 21:47:16 24 4
gpt4 key购买 nike

我正在使用 api 调用格式为“标题”的电影评论:[评分,评论]。使用 json.loads() 函数后,json 列表如下所示:

listResult: {'Shawsank Redemption': [10, 'I love this movie so much'], 'Tropic Thunder': [9,'This was an awesome movie for sure'], 'Ted': [8, 'Hilarious throughout the entire movie']}

我必须返回评分最高的电影,我假设我必须使用 for 循环来找到最大值,但我不确定如何只访问列表中的评分值

最佳答案

假设您的字典名为 listResult(您应该更改它,它不是列表),使用内置的 max 函数并提供自定义键函数:

>>> listResult = {'Shawsank Redemption': [10, 'I love this movie so much'], 'Tropic Thunder': [9,'This was an awesome movie for sure'], 'Ted': [8, 'Hilarious throughout the entire movie']}
>>> max(listResult, key=lambda movie: listResult[movie][0])
'Shawsank Redemption'

keyfunction 用于定义电影名称排序的标准。对于每部电影,我们获取相应列表的第一个元素。

关于python - 你如何调用 json 字典中列表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36776735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com