![](/img/trans.png)
[英]How do I pass multiple dictionary items into a function and return a list?
[英]How do I pass a list response into a Python dictionary and add items only if it does not already exist?
我正在嘗試將 Python 列表響應轉換為 Python 字典,以便稍后可以將 output 轉換為 JSON。我有兩個問題。 第一個是當我嘗試將響應傳遞到字典中時,它將它們添加為響應中顯示的數據標簽項列表。 第二個問題是我只想添加不重復的項目,基本上不會添加兩次相同的名稱。
下面是讓我得到結果的代碼片段。 注意:我決定只展示代碼的最后一部分,因為它很長:
for item in class_resources:
tag = item
print(tag)
這是回應:
Pisces
Animalia
Batoidea
Torpediniformes
Elasmobranchii
Chordata
Neoselachii
Torpedinidae
Torpedo
Torpedo
這是我的 output 字典的片段。 這包括來自其他查詢的其他響應,所以我希望可以添加標簽,而不必將標簽項添加到 data_tags 列表中 append
owner_name = output.get('provider', {}).get('name') or 'NBN Atlas'
owner_url = f"https://registry.nbnatlas.org/public/show/{uid}" if uid is not None else "https://registry.nbnatlas.org/datasets"
owner_api_url = output.get('provider', {}).get(
'uri') or 'https://registry.nbnatlas.org/ws/dataResource'
output_dict = {
'data_providers': [
{
'name': 'NBN Atlas',
'url': 'https://registry.nbnatlas.org/datasets',
'api_url': 'https://registry.nbnatlas.org/ws/dataResource',
'type': 'host'
},
{
'name': owner_name,
'url': owner_url,
'api_url': owner_api_url,
'type': 'owner'
}
],
'data_tags': [{
'type': 'classification',
'tag': tag,
'tag_lower': tag.lower()
}]
}
我當前的 output 是什么:
{ 'data_tags': [ { 'tag': 'Pisces',
'tag_lower': 'pisces',
'type': 'classification'}]}
{ 'data_tags': [ { 'tag': 'Animalia',
'tag_lower': 'animalia',
'type': 'classification'}]}
{ 'data_tags': [ { 'tag': 'Batoidea',
'tag_lower': 'batoidea',
'type': 'classification'}]}
{ 'data_tags': [ { 'tag': 'Torpediniformes',
'tag_lower': 'torpediniformes',
'type': 'classification'}]}
{ 'data_tags': [ { 'tag': 'Elasmobranchii',
'tag_lower': 'elasmobranchii',
'type': 'classification'}]}
{ 'data_tags': [ { 'tag': 'Chordata',
'tag_lower': 'chordata',
'type': 'classification'}]}
{ 'data_tags': [ { 'tag': 'Neoselachii',
'tag_lower': 'neoselachii',
'type': 'classification'}]}
{ 'data_tags': [ { 'tag': 'Torpedinidae',
'tag_lower': 'torpedinidae',
'type': 'classification'}]}
{ 'data_tags': [ { 'tag': 'Torpedo',
'tag_lower': 'torpedo',
'type': 'classification'}]}
{ 'data_tags': [ { 'tag': 'Torpedo',
'tag_lower': 'torpedo',
'type': 'classification'}]}
最后所需的 output 是這樣的:
"data_tags": [
{
"type": "classification",
"tag": "Pisces",
"tag_lower": "pisces"
},
{
"type": "classification",
"tag": "Animalia",
"tag_lower": "animalia"
},
{
"type": "classification",
"tag": "Batoidea",
"tag_lower": "batoidea"
},
...
...
...
],
就像我提到的Torpedo
一樣,副本應該只出現一次。 非常感謝您的幫助。 提前致謝。
您沒有顯示構成字典的代碼部分,但我做了一些可能有幫助的事情。 您可以將分類list
轉換為set
。 Set 不允許重復值,只會默默地刪除多余的值。 然后在每次循環迭代中向您添加條目 output 數據。
class_resources = ['Pisces', 'Pisces', 'Animalia', 'Batoidea',' Torpediniformes', 'Elasmobranchii']
output_dict = {'data_tags': []}
for item in set(class_resources):
output_dict['data_tags'].append({'type': 'classification', 'tag': item, 'tag_lower': item.lower()})
print(output_dict)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.