Johnny Grimes - 11 months ago 38

Python Question

I'm trying to get the count of properties in a dict where poi == true. I've included a C# example for reference, followed by my python code.

What's the correct way of doing this in Python?

C#:

`enron_data.where(x => x.poi == true).count()`

In Python:

`i = 0`

for key,value in enron_data.items():

if value["poi"]:

i = i + 1

print i

enron_data Sample:

`{'METTS MARK': {'salary': 365788, 'to_messages': 807, 'deferral_payments': 'NaN', 'total_payments': 1061827, 'exercised_stock_options': 'NaN', 'bonus': 600000, 'restricted_stock': 585062, 'shared_receipt_with_poi': 702, 'restricted_stock_deferred': 'NaN', 'total_stock_value': 585062, 'expenses': 94299, 'loan_advances': 'NaN', 'from_messages': 29, 'other': 1740, 'from_this_person_to_poi': 1, 'poi': False, 'director_fees': 'NaN', 'deferred_income': 'NaN', 'long_term_incentive': 'NaN', 'email_address': 'mark.metts@enron.com', 'from_poi_to_this_person': 38}}`

Answer Source

I'd probably do something like this assuming that the `poi`

is always `True`

or `False`

.

```
sum(int(v['poi']) for v in enron_data.values())
```

`enron_data.values()`

gives you an iterator that will return values of the dictionary. We're not interested in the keys. For each one, we get the value of the `'poi'`

key. That's what

```
[v['poi'] for v in enron_data.values()]
```

will give you. Assuming that `v['poi']`

is always `True`

or `False`

, I convert it to an integer using `int()`

. `int(True)`

is `1`

and `int(False)`

is `0`

. So, if I run this, I will get a list of `1`

s and `0`

s. Then I `sum`

the entire list to get the count of `1`

s (i.e. `True`

s) using the `sum`

builtin. I drop the `[`

and `]`

because I'm just going to iterate over the list and not index it or anything.

Here's another alternative that occurred to me.

```
sum(1 for v in enron_data.values() if v['poi'] is True)
```