Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calculate percentage of dictionary values

all_brands_scores  = dict(sorted(all_brands_scores.items(), key=lambda item: item[1]))
print(all_brands_scores )

output:

{‘ADATA’: 0, ‘GIGABYTE’: 0, ‘CyberPowerPC’: 0, ‘Hyundai’: 0,
‘Thomson’: 0, ‘KANO’: 1, ‘Alienware’: 1, ‘Razer’: 1, ‘Google’: 1,
‘LG’: 2, ‘HP OMEN’: 5, ‘Acer’: 12, ‘Apple’: 13, ‘Microsoft’: 13,
‘MSI’: 17, ‘Samsung’: 18, ‘Dell’: 24, ‘ASUS’: 54, ‘Lenovo’: 71, ‘HP’:
104}

I have this file(dic) but not as fixed values
How do I calculate the percentage of each type of laptop ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

sum_of_values = sum(data.values())
dict(
    map(
        lambda v: 
            [
                v[0],
                str(v[1] / sum_of_values * 100) + "%"
            ],
            data.items()
        )
    )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading