简体   繁体   English

如何在json字典中的列表中调用值

[英]How do you call a value in a list within a json dictionary

I'm using api's to call movie reviews in the format 'title': [rating, review]. 我正在使用api以“标题”格式调用电影评论:[评分,评论]。 After using a json.loads() function the json list looks something like this: 使用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']}

I have to return the highest rated movie, I'm assuming I have to use a for loop to find the max value but I'm unsure how to access just the rating value within the list 我必须返回收视率最高的电影,假设我必须使用for循环来查找最大值,但是我不确定如何仅访问列表中的收视率值

Assuming your dictionary has the name listResult (which you should change, it's not a list), use the built in max function and provide a custom key-function: 假设您的字典名称为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'

The keyfunction is used to define the criteria by which the movie-names are ordered. 键功能用于定义影片名称排序的标准。 For each movie, we're getting the first element of the corresponding list. 对于每部电影,我们都获得了相应列表的第一个元素。

Here is how to access the rating part of the python list. 这是访问python列表的评价部分的方法。

listResult = {'Movie': [10, "I love this Movie"]}

rating = listResult["Movie"][0]

print rating  // Prints 10

Then just compare each of the ratings to find the max one. 然后只需比较每个评级即可找到最大评级。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您如何检查字典中的字典中是否存在值? - How do you check to see if a value exists in a dictionary within a dictionary? 如何根据用户指定的值从字典列表中的字典中删除键? - How do you delete a key from a dictionary within a list of dictionaries based on a user specified value? 如果满足条件,如何仅在列表中调用字典值? - How do I only call the dictionary value within a list if it meets a condition? 如何在列表中的字典中使用随机 function? - How do you use the random function on a dictionary within a list? 当它是字典中的列表以与 Discord API 一起使用时如何调用该值 - How to call the value when it is a list within a dictionary to use with the Discord API 如何从词典列表中调用词典值? - how do i call dictionary values from within a list of dictionaries ? 如何获取列表列表中的值/用字典值替换它? - How do you get at a value in a list of lists /replace it with a dictionary value? 如何将新列表附加到 JSON 文件中的现有字典 - How do you append a new list to a an existing dictionary in JSON file 对于另一个词典中的一个词典,如何按值对其进行排序并比较该词典中的不同项目 - For a dictionary inside another dictionary how do you sort it by the value and compare different items within the dictionary Python:如何将字典键的值附加在列表中而不是引用中? - Python:how do you append the value of a dictionary key in a list and not the reference?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM