使用多个条件从 Python 中的数组对象传递值
Pass values from array object in Python with multiple criteria
我有以下代码
import requests
import json
parameters = {
"affiliation": "cambridge university"
}
response= requests.get("https://api.ror.org/organizations", params=parameters)
print (response.json())
这个 returns me JSON 包含项目数组的对象 - this is the link to the result in API as its very long to paste it all here
集合中的第一个结果的值为“真”。根据这个结果,我想为“子”元素传递“名称”、“别名”和“标签”。
根据 API 的响应,我想为标记为“已选择”的第一个结果传递如下内容:true:
"name":"University of Cambridge"
"aliases":["Cambridge University"]
"label":"Cambridge University Press","type":"Child"
"label":"Cambridge–MIT Institute","type":"Child"
etc. including each of the "type":"Child" relationships
我完全迷失了尝试这样做。我尝试过不同的东西,但我能得到的最好的是“None”。如何使用“真”标准在数组中移动并获得相关结果?
希望我已经正确理解您的问题。要过滤结果,您可以使用下一个示例:
import json
import requests
parameters = {"affiliation": "cambridge university"}
response = requests.get("https://api.ror.org/organizations", params=parameters)
result = []
for item in response.json()["items"]:
if item["chosen"] == True:
d = {"name": item["organization"]["name"]}
d["aliases"] = item["organization"]["aliases"]
d["labels"] = []
for r in item["organization"]["relationships"]:
if r["type"] == "Child":
d["labels"].append(r)
del r["id"]
result.append(d)
print(result)
打印:
[
{
"name": "University of Cambridge",
"aliases": ["Cambridge University"],
"labels": [
{"label": "Cambridge University Press", "type": "Child"},
{"label": "Cambridge–MIT Institute", "type": "Child"},
{"label": "Cancer Research UK Cambridge Center", "type": "Child"},
{
"label": "Centre for the Observation and Modelling of Earthquakes, Volcanoes and Tectonics",
"type": "Child",
},
{"label": "Hutchison/MRC Research Centre", "type": "Child"},
{"label": "MRC Biostatistics Unit", "type": "Child"},
{"label": "MRC Cognition and Brain Sciences Unit", "type": "Child"},
{"label": "MRC Epidemiology Unit", "type": "Child"},
{"label": "MRC Human Nutrition Research", "type": "Child"},
{"label": "MRC Mitochondrial Biology Unit", "type": "Child"},
{"label": "MRC Toxicology Unit", "type": "Child"},
{"label": "Sedgwick Museum of Earth Sciences", "type": "Child"},
{
"label": "The Cambridge Centre for Advanced Research and Education in Singapore",
"type": "Child",
},
{
"label": "Wellcome/Cancer Research UK Gurdon Institute",
"type": "Child",
},
{
"label": "Wellcome/MRC Cambridge Stem Cell Institute",
"type": "Child",
},
{
"label": "Wellcome/MRC Institute of Metabolic Science",
"type": "Child",
},
],
}
]
编辑:删除了id
键
我有以下代码
import requests
import json
parameters = {
"affiliation": "cambridge university"
}
response= requests.get("https://api.ror.org/organizations", params=parameters)
print (response.json())
这个 returns me JSON 包含项目数组的对象 - this is the link to the result in API as its very long to paste it all here
集合中的第一个结果的值为“真”。根据这个结果,我想为“子”元素传递“名称”、“别名”和“标签”。
根据 API 的响应,我想为标记为“已选择”的第一个结果传递如下内容:true:
"name":"University of Cambridge"
"aliases":["Cambridge University"]
"label":"Cambridge University Press","type":"Child"
"label":"Cambridge–MIT Institute","type":"Child"
etc. including each of the "type":"Child" relationships
我完全迷失了尝试这样做。我尝试过不同的东西,但我能得到的最好的是“None”。如何使用“真”标准在数组中移动并获得相关结果?
希望我已经正确理解您的问题。要过滤结果,您可以使用下一个示例:
import json
import requests
parameters = {"affiliation": "cambridge university"}
response = requests.get("https://api.ror.org/organizations", params=parameters)
result = []
for item in response.json()["items"]:
if item["chosen"] == True:
d = {"name": item["organization"]["name"]}
d["aliases"] = item["organization"]["aliases"]
d["labels"] = []
for r in item["organization"]["relationships"]:
if r["type"] == "Child":
d["labels"].append(r)
del r["id"]
result.append(d)
print(result)
打印:
[
{
"name": "University of Cambridge",
"aliases": ["Cambridge University"],
"labels": [
{"label": "Cambridge University Press", "type": "Child"},
{"label": "Cambridge–MIT Institute", "type": "Child"},
{"label": "Cancer Research UK Cambridge Center", "type": "Child"},
{
"label": "Centre for the Observation and Modelling of Earthquakes, Volcanoes and Tectonics",
"type": "Child",
},
{"label": "Hutchison/MRC Research Centre", "type": "Child"},
{"label": "MRC Biostatistics Unit", "type": "Child"},
{"label": "MRC Cognition and Brain Sciences Unit", "type": "Child"},
{"label": "MRC Epidemiology Unit", "type": "Child"},
{"label": "MRC Human Nutrition Research", "type": "Child"},
{"label": "MRC Mitochondrial Biology Unit", "type": "Child"},
{"label": "MRC Toxicology Unit", "type": "Child"},
{"label": "Sedgwick Museum of Earth Sciences", "type": "Child"},
{
"label": "The Cambridge Centre for Advanced Research and Education in Singapore",
"type": "Child",
},
{
"label": "Wellcome/Cancer Research UK Gurdon Institute",
"type": "Child",
},
{
"label": "Wellcome/MRC Cambridge Stem Cell Institute",
"type": "Child",
},
{
"label": "Wellcome/MRC Institute of Metabolic Science",
"type": "Child",
},
],
}
]
编辑:删除了id
键