Search code examples
pythonlistdictionaryfor-loopfind-occurrences

Python: Count the occurence of value in nested dictionary if other value is true


I have a nested dictionary that looks like this:

{
1: {'Name': {'John'}, 'Answer': {'yes'}, 'Country': {'USA'}}, 
2: {'Name': {'Julia'}, 'Answer': {'no'}, 'Country': {'Hong Kong'}}
3: {'Name': {'Adam'}, 'Answer': {'yes'}, 'Country': {'Hong Kong'}}
}

I now need to get the occurrence of each country and the number of people who answered yes or no. Currently, I only collect the number of occurrences in each country:

nationalities = ['USA', 'Hong Kong', 'France' ...]
for countries in nationalities:
    cnt =[item for l in [v2 for v1 in dictionary1.values() for v2 in v1.values()] for item in l].count(countries)
    result.append(countries + ': ' + str(cnt))

so using my datasheet I get something like

['Hong Kong: 2', 'France: 2', 'Italy: 3']

However, I would like to get the proportion of the people who answered yes and who answered no. Such that I get a list in the form of ['Hong Kong: 2 1 1'] where the first number would be total and the second and third would be yes and no respectively

Thanks for any help


Solution

  • a={
    1: {'Name': {'John'}, 'Answer': {'yes'}, 'Country': {'USA'}}, 
    2: {'Name': {'Julia'}, 'Answer': {'no'}, 'Country': {'Hong Kong'}},
    3: {'Name': {'Adam'}, 'Answer': {'yes'}, 'Country': {'Hong Kong'}}
    }
    results=[]
    nationalities = ['USA', 'Hong Kong', 'France']
    for country in nationalities:
        countryyes=0
        countryno=0
        for row in a.values():
            if str(row['Country'])[2:-2] == country:
                if str(row['Answer'])[2:-2] == 'yes':
                    countryyes+=1
                if str(row['Answer'])[2:-2] == 'no':
                    countryno+=1
        results.append(country+': '+str(countryyes+countryno)+' '+str(countryyes)+' '+str(countryno))
    

    I want to make a couple notes. First, I changed countries to country (it's abnormal to use a plural name in a for loop like that). Second, I wanted to comment and say that if your code above you have the name, answer, and country each in a set and I think you would be better off changing that to just having it as a string.