Take the 2-minute tour ×

I am building a financial model and stuck on last bit of the model where I have to produce graphs from the output of the functions. Below is my final function and I didn't put other functions as the code for them is very large. The model shows how the (customers, orders, netsales etc) evolve per day through out the year for different customer type. So if run my function for say customer type 12, it will run from day 1 up to day 365 showing how the customers or other stuff have grown.

def show_all_states_list(cust_type):
    s  = get_state0(cust_type)    
    all_states =[]
    for day in range(365):
        s = state_evolution(s, cust_type)
    return all_states

Output from this function for day 1 is(not including all the output) when I run this in python:

s = show_all_states_list(12)
>>> s[0]
{'day': 1,'custtypeA_nondp': 50.99574423351457,'delfeeA_dp_cost': 0.45111124745572373, 'today_ordersA_nondp':0,'profit_current_vs_with_dp ': -5.4141382286616135, 'gross_profit_nondp_dp': 35441.52120017628, 'fixed_costs_nondp_dp ': -56407.52069653516, 'combine_nondp_dp_profit': -5642.694138228653, 'today_netsalesA_nondp': 0, 'gross_turnover_dp_nondp_cust': 142363.5991835414, 'del_nondp_dp_turnover': 28036.11144103729, 'var_costs_nondp_dp': -12712.806082907056, 'custtypeA_dp': 0.004255766485431356, 'ordersA_dp': 0.4080111809905219, 'netsalesA_nondp': 114327.45898877166, 'ordersA_nondp': 4444.629080744554, 'today_delfeeA_nondp': 0, 'delfeeA_nondp': 28035.660329789833, 'totords_dp_nondp': 4445.037091925545,  'today_ordersA_dp': 0, 'gross_turnover_dp_cust': 0.4798649799246596, 'decreased_bsktsize_dp': 0.07047290321586513, 'custtypeA': 51.0, 'net_turnover_dp': 0.028753732468935884, 'current_vs_dp_gross_sales': -11.400816458597546}

what I want is that from this outputs for all 365 days take out each key values such as 'custtypeA_nondp'(so I will 365 values for this) and make it into a new list so that I can get a plot of days against 'custtypeA_nondp' which will show my output graphically.

I know how to plot a scatter graph but don't know how would you extract information from a functions output.

I would really appreciate help on this.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Dictionaries have a convienient values() method that does what you need:

>>> d = {'day': 1,'custtypeA_nondp': 50.99574423351457,'delfeeA_dp_cost': 0.45111124745572373}
>>> d.values()
[0.45111124745572373, 1, 50.995744233514571]

The complement is the keys() method:

>>> d.keys()
['delfeeA_dp_cost', 'day', 'custtypeA_nondp']

Your method returns a list of dictionaries, so if want only to have a list of custtypeA_nondp of all the dictionaries in the returned list:

vals = list(x['custtypeA_nondp'] for x in s)
share|improve this answer
If you see the above function I have created a list of dicts –  Safe Khan Jul 25 '13 at 11:05
Thank you very much. –  Safe Khan Jul 25 '13 at 11:14
add comment
all_states = show_all_states_list(12)
typeAs = [d['custtypeA_nondp'] for d in all_states]

Or the same using operator.itemgetter():

from operator import itemgetter

typeAs = map(itemgetter('custtypeA_nondp'), all_states)
share|improve this answer
add comment

Your Answer


