简体   繁体   中英

How do I pass a list response into a Python dictionary and add items only if it does not already exist?

I'm trying to turn a Python list response into a Python dictionary so I can later output it as JSON. I have two issues. The first is when I try to pass the response into the dictionary, it adds them as a list of data tags items as shown in the response. The second issue is that I would like to only add non duplicate items only, basically not to add the same name twice.

Below is the snippet of code that gets me the result. Note: I have decided to only show the last part of the code since it is very lengthy:

for item in class_resources:
    tag = item
    print(tag)

And this is the response:

Pisces
Animalia
Batoidea
Torpediniformes
Elasmobranchii
Chordata
Neoselachii
Torpedinidae
Torpedo
Torpedo

This is the snippet of my output dictionary. This includes other responses from other queries so I was hoping the tags could be added without having to append the tags items to the data_tags list

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()
    }]
}

What my current output is:

{   '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'}]}

And finally the desired output is something like this:

"data_tags": [
    {
        "type": "classification",
        "tag": "Pisces",
        "tag_lower": "pisces"
    },
    {
        "type": "classification",
        "tag": "Animalia",
        "tag_lower": "animalia"
    },
    {
        "type": "classification",
        "tag": "Batoidea",
        "tag_lower": "batoidea"
    },
    ...
    ...
    ...
],

And like I mentioned Torpedo as a duplicate should only appear once. Your help will be much appreciated. Thanks in advance.

You're not showing the part of code where you form the dictionary, but I made something to possibly help. You can cast the list of classifications to a set . Set doesn't allow duplicate values and will just silently get rid of the extra ones. Then add entries to you output data in every loop iteration.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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