简体   繁体   English

具有多个键的 Python 字典列表

[英]Python dictionary list with multiple keys

I am fairly new to Python, so I apologize for any mistakes found within my post.我对 Python 还很陌生,因此对于我在帖子中发现的任何错误,我深表歉意。 I have recently been playing around with APIs with Python and came across an API that responds to my request with a list of dictionaries for films for the respective studio providing the API (Not sure if it is a JSON list of dictionaries) From the list of dictionaries I am trying to create a for-loop to get rotten tomato scores greater than 90, but I would also like to get the corresponding title of the movie within the for-loop.我最近一直在玩 Python 的 API,遇到了一个 API,该 API 响应我的请求,其中包含提供 API 的各个工作室的电影词典列表(不确定它是否是 JSON 词典列表)从列表中字典 我正在尝试创建一个 for 循环以获得大于 90 的烂番茄分数,但我也想在 for 循环中获得电影的相应标题。 I have tried adding if key == 'rt_score' and 'title' , but I don't know if this is the correct approach and whether it would lead me to the correct solution.我曾尝试添加if key == 'rt_score' and 'title' ,但我不知道这是否是正确的方法以及它是否会引导我找到正确的解决方案。 Here is my code:这是我的代码:

import requests
import json

response = requests.get("https://ghibliapi.herokuapp.com/films")

print(response.status_code)
json_response = response.json()

for dicti in json_response:
    for key in dicti:
        if key == 'rt_score':
            rt_score = int(dicti[key])
            if rt_score > 90:
                print(str(key) + ': ' + str(rt_score))

This is what the JSON list of dictionaries looks like:这是字典的 JSON 列表的样子:

[
   {
      "description": "The orphan Sheeta inherited a mysterious crystal that links her to the mythical sky-kingdom of Laputa. With the help of resourceful Pazu and a rollicking band of sky pirates, she makes her way to the ruins of the once-great civilization. Sheeta and Pazu must outwit the evil Muska, who plans to use Laputa's science to make himself ruler of the world.",
      "director": "Hayao Miyazaki",
      "id": "2baf70d1-42bb-4437-b551-e5fed5a87abe",
      "locations": [
         "https://ghibliapi.herokuapp.com/locations/"
      ],
      "people": [
         "https://ghibliapi.herokuapp.com/people/"
      ],
      "producer": "Isao Takahata",
      "release_date": "1986",
      "rt_score": "95",
      "species": [
         "https://ghibliapi.herokuapp.com/species/af3910a6-429f-4c74-9ad5-dfe1c4aa04f2"
      ],
      "title": "Castle in the Sky",
      "url": "https://ghibliapi.herokuapp.com/films/2baf70d1-42bb-4437-b551-e5fed5a87abe",
      "vehicles": [
         "https://ghibliapi.herokuapp.com/vehicles/"
      ]
   },

To iterate over the keys you need to call .keys() on the dictionary, but you do not need to do that.要迭代您需要在字典上调用.keys()的键,但您不需要这样做。 Just use the key with the dictionary like shown below.只需将密钥与字典一起使用,如下所示。

for dict_ in json_response:
    if float(dict_['rt_score']) > 90:
        title = dict_['title']
        print(title)

You can access the title within your for-loop by dicti['title'] .您可以通过dicti['title']访问 for 循环中dicti['title']

Full example using your code:使用您的代码的完整示例:

import requests
import json

response = requests.get("https://ghibliapi.herokuapp.com/films")

print(response.status_code)
json_response = response.json()

for dicti in json_response:
    for key in dicti:
        if key == 'rt_score':
            rt_score = int(dicti[key])
            if rt_score > 90:
                print(dicti['title'] + ' - ' str(key) + ': ' + str(rt_score))

A simple way:一个简单的方法:

for film in json_response:
    if int(film['rt_score']) > 90:
        print(f"{film['title']}: {film['rt_score']}")

This uses a f-string for printing, so needs at least Python 3.6.这使用 f 字符串进行打印,因此至少需要 Python 3.6。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM