-1

I have a list that looks like this (it's Enron data, and therefore publicly available, so pretty sure I'm not breaking any rules here):

{
   'METTS MARK':{
      'salary':365788,
      'to_messages':807,
      'deferral_payments':'NaN',
      'total_payments':1061827,
      'loan_advances':'NaN',
      'bonus':600000,
      'email_address':'mark.metts@enron.com',
      'restricted_stock_deferred':'NaN',
      'deferred_income':'NaN',
      'total_stock_value':585062,
      'expenses':94299,
      'from_poi_to_this_person':38,
      'exercised_stock_options':'NaN',
      'from_messages':29,
      'other':1740,
      'from_this_person_to_poi':1,
      'poi':False,
      'long_term_incentive':'NaN',
      'shared_receipt_with_poi':702,
      'restricted_stock':585062,
      'director_fees':'NaN'
   },
   'BAXTER JOHN C':{
      'salary':267102,
      'to_messages':'NaN',
      'deferral_payments':1295738,
      'total_payments':5634343,
      'loan_advances':'NaN',
      'bonus':1200000,
      'email_address':'NaN',
      'restricted_stock_deferred':'NaN',
      'deferred_income':-1386055,
      'total_stock_value':10623258,
      'expenses':11200,
      'from_poi_to_this_person':'NaN',
      'exercised_stock_options':6680544,
      'from_messages':'NaN',
      'other':2660303,
      'from_this_person_to_poi':'NaN',
      'poi':False,
      'long_term_incentive':1586055,
      'shared_receipt_with_poi':'NaN',
      'restricted_stock':3942714,
      'director_fees':'NaN'
   },
   'ELLIOTT STEVEN':{
      'salary':170941,
      'to_messages':'NaN',
      'deferral_payments':'NaN',
      'total_payments':211725,
      'loan_advances':'NaN',
      'bonus':350000,
      'email_address':'steven.elliott@enron.com',
      'restricted_stock_deferred':'NaN',
      'deferred_income':-400729,
      'total_stock_value':6678735,
      'expenses':78552,
      'from_poi_to_this_person':'NaN',
      'exercised_stock_options':4890344,
      'from_messages':'NaN',
      'other':12961,
      'from_this_person_to_poi':'NaN',
      'poi':False,
      'long_term_incentive':'NaN',
      'shared_receipt_with_poi':'NaN',
      'restricted_stock':1788391,
      'director_fees':'NaN'
   }
}

I'm trying to make a list of all exercised_stock_options values, and then I want to find the non NaN values and perform a max/min.

I did this but I get an incorrect answer, and I'm not sure why:

eso = []
for k,v in data_dict.items():
    # name.append[str(k)]
    eso.append(v['exercised_stock_options'])

# Remove NaN values in list
eso = [x for x in eso if str(x) != 'NaN']

print(eso)

sorted(eso)
print(eso[0])
print(eso[-1])

But then I saw this answer and it's correct, but I can't see what I'm doing wrong?

stock = []
for i in data_dict:
    if (data_dict[i]["exercised_stock_options"]=='NaN'):
        pass
    else:
        stock.append(float(data_dict[i]["exercised_stock_options"]))

ma = max(stock)
mi = min(stock)

print ("Exercised stock options maximum: ", ma, " minimum: ", mi)
urban
  • 5,392
  • 3
  • 19
  • 45
Aaraeus
  • 1,105
  • 3
  • 14
  • 26
  • 6
    `sorted` doesn't change the list, it simply returns a sorted version of it. Replace `sorted(eso)` with `eso.sort()`, which will destructively sort `eso`. – Tom Karzes Jan 06 '19 at 12:48
  • 1
    Possible duplicate of [Does Python sorted() actually CHANGE the list being sorted?](https://stackoverflow.com/questions/30706746/does-python-sorted-actually-change-the-list-being-sorted) – Dani Mesejo Jan 06 '19 at 12:51
  • Possible duplicate of [What is the difference between \`sorted(list)\` vs \`list.sort()\`?](https://stackoverflow.com/questions/22442378/what-is-the-difference-between-sortedlist-vs-list-sort) – mkrieger1 Jan 06 '19 at 12:57
  • Alternatively `eso = sorted(eso)` – Michael Jan 06 '19 at 12:59
  • Another point, your list is string whereas the solution you showed is casting the values to float. Not sure if sorting like this will affect the result. – Michael Jan 06 '19 at 13:01
  • @Michael The `sort` method is preferred (for efficiency) in cases where the original list order isn't needed, since it avoids an unnecessary copy of the list. – Tom Karzes Jan 06 '19 at 15:11

1 Answers1

0

In the first case the values are sorted as strings, however, you want to sort them as numbers. This is why implicit type casting (with float function) is used in the second case.

You may want to change line eso.append(v['exercised_stock_options']) to eso.append(float(v['exercised_stock_options'])). But you should get rid of 'NaN' values first.

Damir
  • 309
  • 2
  • 5