[英]How to get the count of distinct values in dictionary python
Dictionary is below字典如下
todos = [{'userId': 1, 'id': 1, 'title': 'A', 'completed': False},
{'userId': 1, 'id': 2, 'title': 'B ', 'completed': False},
{'userId': 1, 'id': 1, 'title': 'C', 'completed': False},
{'userId': 1, 'id': 2, 'title': 'A', 'completed': True},
{'userId': 2, 'id': 1,'title': 'B', 'completed': False}]
Code is below to print the values下面的代码用于打印值
for i in todos:
print((i['userId']))
for i in todos:
print((i['title']))
for i in todos:
print((i['completed']))
Expected out is预计出来的是
{"userid" : 2, "title":3, "completed" : 2}
I just need the distinct count of values我只需要不同的值计数
userid
there are only 2 because 1
and 2
values are present userid
只有 2 个,因为存在1
和2
值title
is 3 as values are A
, B
, C
title
为 3,因为值为A
, B
, C
completed
is 2 as values are True
and False
completed
为 2,因为值为True
和False
Use list comprehension and length of the set like so:使用列表理解和集合的长度,如下所示:
cnt = {}
for todos_key in todos[0].keys():
cnt[todos_key] = len(set([item[todos_key] for item in todos]))
print(cnt)
# {'userId': 2, 'id': 2, 'title': 4, 'completed': 2}
You can use pandas
to do so.您可以使用
pandas
来执行此操作。
Just create a data-frame from the todos
dictionary and then using the nunique
method.只需从
todos
字典创建一个数据框,然后使用nunique
方法。
import pandas as pd
todos = [{'userId': 1, 'id': 1, 'title': 'A', 'completed': False},
{'userId': 1, 'id': 2, 'title': 'B ', 'completed': False},
{'userId': 1, 'id': 1, 'title': 'C', 'completed': False},
{'userId': 1, 'id': 2, 'title': 'A', 'completed': True},
{'userId': 2, 'id': 1,'title': 'B', 'completed': False}]
df = pd.DataFrame(todos)
df['userId'].nunique()
df['title'].nunique()
df['completed'].nunique()
Notice that the second dicionay title is B
and not B
so you might need to handle it.请注意,第二个字典标题是
B
而不是B
,因此您可能需要处理它。
you can do the following without importing pandas:您可以在不导入 pandas 的情况下执行以下操作:
todos = [{'userId': 1, 'id': 1, 'title': 'A', 'completed': False},
{'userId': 1, 'id': 2, 'title': 'B ', 'completed': False},
{'userId': 1, 'id': 1, 'title': 'C', 'completed': False},
{'userId': 1, 'id': 2, 'title': 'A', 'completed': True},
{'userId': 2, 'id': 1,'title': 'B', 'completed': False}]
from collections import defaultdict
d = defaultdict(lambda:[])
for todo in todos:
for k,v in todo.items():
d[k].append(v)
d = {k:len(set(v)) for k,v in d.items()}
print(d)
output: output:
{'userId': 2, 'id': 2, 'title': 4, 'completed': 2}
EXPLAINATION: first you create a defaultdict the initializes default values when not existed d = defaultdict(lambda:[])
解释:首先你创建一个 defaultdict 在不存在时初始化默认值
d = defaultdict(lambda:[])
create a dict of lists instead of dict of lists创建列表的字典而不是列表的字典
for todo in todos:
for k,v in todo.items():
d[k].append(v)
then you extract the uniquevalues with set and measure the length {k:len(set(v)) for k,v in d.items()}
.然后你用 set 提取唯一值并测量长度
{k:len(set(v)) for k,v in d.items()}
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.