简体   繁体   中英

How do I add values in a list that is inside a dictionary?

I have a JSON file that called jdata_list, it looks like:

jdata_list = [{'API': 'AdoptAPet', 'Description': 'Resource to help get pets adopted', 'Auth': 'apiKey', 'HTTPS': True, 'Cors': 'yes', 'Link': 'https://www.adoptapet.com/public/apis/pet_list.html', 'Category': 'Animals'}, {'API': 'HTTP Cat', 'Description': 'Cat for every HTTP Status', 'Auth': '', 'HTTPS': True, 'Cors': 'yes', 'Link': 'https://http.cat/', 'Category': 'Animals'}, {'API': 'RandomDuck', 'Description': 'Random pictures of ducks', 'Auth': '', 'HTTPS': True, 'Cors': 'no', 'Link': 'https://random-d.uk/api', 'Category': 'Animals'}, {'API': 'AnimeChan', 'Description': 'Anime quotes (over 10k+)', 'Auth': '', 'HTTPS': True, 'Cors': 'no', 'Link': 'https://github.com/RocktimSaikia/anime-chan', 'Category': 'Anime'}, {'API': 'NekosBest', 'Description': 'Neko Images & Anime roleplaying GIFs', 'Auth': '', 'HTTPS': True, 'Cors': 'yes', 'Link': 'https://docs.nekos.best', 'Category': 'Anime'}, {'API': 'MalDatabase', 'Description': 'Provide malware datasets and threat intelligence feeds', 'Auth': 'apiKey', 'HTTPS': True, 'Cors': 'unknown', 'Link': 'https://maldatabase.com/api-doc.html', 'Category': 'Anti-Malware'}, {'API': 'Web of Trust', 'Description': 'IP/domain/URL reputation', 'Auth': 'apiKey', 'HTTPS': True, 'Cors': 'unknown', 'Link': 'https://support.mywot.com/hc/en-us/sections/360004477734-API-', 'Category': 'Anti-Malware'}, {'API': 'Icon Horse', 'Description': 'Favicons for any website, with fallbacks', 'Auth': '', 'HTTPS': True, 'Cors': 'yes', 'Link': 'https://icon.horse', 'Category': 'Art & Design'}, {'API': 'xColors', 'Description': 'Generate & convert colors', 'Auth': '', 'HTTPS': True, 'Cors': 'yes', 'Link': 'https://x-colors.herokuapp.com/', 'Category': 'Art & Design'}, {'API': 'Chainpoint', 'Description': 'Chainpoint is a global network for anchoring data to the Bitcoin blockchain', 'Auth': '', 'HTTPS': True, 'Cors': 'unknown', 'Link': 'https://tierion.com/chainpoint/', 'Category': 'Blockchain'}]

I am trying store the links that have the same category inside a dictionary. Where the list of links is the values and the key being the category and to count the number of APIs classified under same category

# Get the categories of file
list = []
jdata_categories_list = list.copy()
for items in jdata_list:
  category = items.get('Category')
  if category not in jdata_categories_list:
        jdata_categories_list.append(category)

#Create dictionary with list values to store links
jdata_categories = dict.fromkeys(jdata_categories_list,[])

#Loop through items in list and add them to the correct list in dictionary
for category in jdata_categories:
    for d in jdata_list[0:100:5]:
        d_link = d.get('Link')
        d_category = d.get('Category')
        cata_list = jdata_categories[d_category]
        while category == d_category:
            cata_list.append(d_link)
            print(f'I added {d_link} to the {d_category}')
            break

But my code adds the link to every category instead of the one it belongs to, even though the print statement says the correct category.

How do I add the link to the correct category list inside the dictionary?

This is what I did to get what you wanted.

json_list = [{"name": "demon slayer", "category": "anime"}, {"name": "cow", "category": "animal"},
             {"category": "movies", "name": "iron man"}, {"name": "thor", "category": "movies"},
             {"name": "naruto", "category": "anime"}]

category_dictionary = {data["category"]: [] for data in json_list}

for data in json_list:
    category_dictionary[data['category']].append(data["name"])

print(category_dictionary)

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