在使用 python 从字典列表中提取值计数时需要帮助
Posted
技术标签:
【中文标题】在使用 python 从字典列表中提取值计数时需要帮助【英文标题】:Need help in extracting value counts from a list of dictionaries using python 【发布时间】:2021-09-21 15:01:04 【问题描述】:我希望使用 Python 计算字典列表中的键值的百分比及其对应的计数以及值及其对应的计数。
使用下面的代码来提取键及其计数。需要帮助来扩展它以提取值及其相应的计数。
数据如下所示:
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.
我想要这样的输出:
Name: 10
'Tom': 2, 'Mark': 3, 'Pam': 1,'Lena': 3, 'Ben': 2
City:4
'London': 1, 'California': 1, 'NewYork': 2
我是新手,谁能帮帮我?
【问题讨论】:
【参考方案1】:试试:
from collections import Counter, defaultdict
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",
]
cnt = defaultdict(Counter)
for p in people:
if not isinstance(p, dict): # <-- make sure the items are dicts
continue
for k, v in p.items():
cnt[k].update([v])
for k, v in cnt.items():
print(k, sum(cnt[k].values()))
for kk, vv in v.items():
print(": ".format(kk, vv), end=" ")
print("\n")
打印:
name 8
Tom: 2 Mark: 1 Pam: 1 Lena: 2 Ben: 2
age 4
10: 1 5: 1 7: 1 17: 1
city 5
NewYork: 1 London: 1 California: 1 Colombo: 2
country 3
Japan: 1 Italy: 2
hight 1
163: 1
weight 1
45: 1
gender 2
Female: 1 Male: 1
更新:添加了检查值的类型为dict
【讨论】:
我收到 AttributeError: 'str' object has no attribute 'items' while qpplying it on large data set @Sherlock 确保列表people
仅包含字典,而不包含字符串。
我无法控制数据...有什么办法可以避免它们【参考方案2】:
您可以尝试对每个值使用list.count
方法来获取每个值的计数:
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:
out[k] = out.get(k, []) + [d[k]]
return out
d = getKeyCount(people)
def display(d, key):
vals = d[key]
print("Name:", len(vals))
print(', '.join(f"val: vals.count(val)" for val in set(vals)))
display(d, 'name')
display(d, 'city')
输出:
Name: 8
Mark: 1, Ben: 2, Tom: 2, Pam: 1, Lena: 2
City: 5
London: 1, NewYork: 1, Colombo: 2, California: 1
【讨论】:
我有 100 多个键...可以循环它们如何获得所有结果 @Sherlock 你可以这样做:for key in d: display(d, key)
以上是关于在使用 python 从字典列表中提取值计数时需要帮助的主要内容,如果未能解决你的问题,请参考以下文章