Python Counting countries in dictionary

2024/10/16 3:28:16

I'm writing a function that counts the number of times a country appears in a dictionary and returns the country that appeared the most. If more then one country appears the most then it should return a list of the countries.

Example dictionary:

{'Leonardo da Vinci': [("Portrait of Isabella d'Este", 1499, 63.0, 46.0, 'chalk', 'France'), ('The Last Supper', 1495, 460.0, 880.0, 'tempera', 'Italy')], 'Pablo Picasso': [('Guernica', 1937, 349.0, 776.0, 'oil paint', 'Spain')]}

Since France, Italy, and Spain all only appear once in this dictionary the function should return

countries_appeared_most(dictionary1())['France', 'Italy', 'Spain']

If one of those countries had instead appeared 2 or 3 times the function would return just that country. The current code I have below instead searches for the artist that appears the most but I believe a couple small changes could help me instead return the country that appears the most. Does anyone have advice on how to do this? Thanks for the help

Code:

def countries_appeared_most(db):if not db:return Nonemaxcount = max(len(v) for v in db.values())themax = [k for k, v in db.items() if len(v) == maxcount]themax.sort()return themax
Answer
counter = {}
for painting_list in db.values():for painting in painting_list:country = painting[-1]counter[country] = counter.get(country, 0) + 1
maxcount = max(counter.values())
themax = [k for k, count in counter.items() if count == maxcount]
https://en.xdnf.cn/q/117759.html

Related Q&A

How to wait for any socket to have data?

Im implementing a socket-client which opens several sockets at the same time. Any socket may have data at a different time and I want to execute code when any socket has data and is readable.Im not sur…

Retrieving ad URLs

Im looking for a way to retrieve the ad URLs for this website. http://www.quiltingboard.com/resources/What I want to do is probably write a script to continuously refresh the page and grab the ad URLs.…

Extract data (likes) from JSON API using Python

I want to pull the number of likes for my project. Heres my code: import facepy from facepy import GraphAPI from bs4 import BeautifulSoup import json access = CAACEdEose0cBAE3IL99IreDeAfqaVZBOje8ZCqIhf…

No nested nodes. How to get one piece of information and then to get additional info respectively?

For the code below I need to get dates and their times+hrefs+formats+...(not shown) respectively.<div class="showtimes"><h2>The Little Prince</h2><div class="poster&…

I need help changing the color of text in python

Hey I need help with coloring the text in a program I am making. It is a password program and I am trying to make the denied and granted red and green when they appear. Here is the program so far:passw…

How do I use Liclipse to write a ParaView script?

Ive tried following the directions here without success. Here are some of my environment variables:Path: C:\Python34\;C:\Python34\Scripts;...;C:\Program Files (x86)\ParaView 4.3.1\lib\paraview-4.3\site…

List of tuples to nested dictionary without overriding

I need to convert the above list of tuples to nested dictionary without overwriting the value as below in python[(a, 1),(b, true),(b, none),(a, 2),(b, true),(a, 3),(b, false)]{a: {1 : { b : (true,none)…

Rotate matplotlib pyplot with curve by 90 degrees

I have plot with one line as this:import numpy as np import matplotlib.pyplot as pla = np.array([4, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9]) b = np.array([i/len(a) for i in range(1,…

Reading csv file and returning as dictionary

Ive written a function that currently reads a file correctly but there are a couple of problems. It needs to be returned as a dictionary where the keys are artist names and the values are lists of tupl…

Spark converting Pandas df to S3

Currently i am using Spark along with Pandas framework. How can I convert Pandas Dataframe in a convenient way which can be written to s3. I have tried below option but I get error as df is Pandas dat…