I have a dictionary named "enron_data". In the dictionary, each key has a variable named "poi". I would like to count how many of the keys have poi=1. Like enron_data[person_name]["poi"]==1.
Dictionary keys are some names and there are 146 names. I want to count how many of them have poi variable equal to one. Thank a lot.
Asked
Active
Viewed 99 times
0
-
Please post the input data. – Mihai Alexandru-Ionut May 05 '18 at 12:11
-
You are telling what is the solution , just try a loop and iterate through dictionary and check the condition , if it met increase some counter. – ᴀʀᴍᴀɴ May 05 '18 at 12:11
-
Thanks for the answers. The input data is a material from an udacity project. I tried to iterate it but I did not how to it for a dictionary. Because the keys are not integers, instead they are strings. I tried to enumerate the dictionary to access the correct elements of each key in each iteration but it did not work. – May 09 '18 at 22:21
1 Answers
1
You an sum the equality checks for the entire dictionary. This works because True == 1
and False == 0
sum(v.get("poi", 0) == 1 for v in enron_data.values())

Patrick Haugh
- 59,226
- 13
- 88
- 96
-
I found the answer. here it is the link:https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa. but actually was looking how to use the for structure you pointed above. It works. Thanks a lot – May 05 '18 at 12:18