I am looking to calculate the % of values against keys from a List of dictionaries and their corresponding counts along with values and their corresponding count using Python.
Used below code to extract keys and their count. Need help in expanding it to extract values and their corresponding count.
The Data looks like this:
people = [
{"name": "Tom", "age": 10, "city": "NewYork"},
{"name": "Mark", "age": 5, "country": "Japan"},
{"name": "Pam", "age": 7, "city": "London"},
{"name": "Tom", "hight": 163, "city": "California"},
{"name": "Lena", "weight": 45, "country": "Italy"},
{"name": "Ben", "age": 17, "city": "Colombo"},
{"name": "Lena", "gender": "Female", "country": "Italy"},
{"name": "Ben", "gender": "Male", "city": "Colombo"},
]
def getKeyCount(lst):
out = {}
for d in lst:
for k in d.keys():
out[k] = out.get(k, 0) + 1
return out
def getValCount(lst):
out = {}
for d in lst:
for v in d.values():
out[v] = out.get(v, 0) + 1
return out
getKeyCount(people)
# {'name': 8, 'age': 4, 'city': 5, 'country': 3,
# 'hight': 1, 'weight': 1, 'gender': 2}
getValCount(people)
# {'Tom': 2, 'NewYork': 1, 'Mark': 1, 'Japan': 1, 'Pam': 1,
# 'London': 1, 'California': 1, etc.}
I want output like this:
Name: 10
'Tom': 2, 'Mark': 3, 'Pam': 1,'Lena': 3, 'Ben': 2
City:4
'London': 1, 'California': 1, 'NewYork': 2
I am new to this, can someone help me?