dictionary - creating nested dictionaries in python from database query -
i have information i'm retrieving database:
with fields like
|portfolio| date | sector| industry | ticker | price | postion|
and data like
("us", "2013-01", "consumer","retail","pm",10,1000) ("us", "2013-01", "consumer", "retail", "jcp", 15, 1500) ...
how can turn dictionary looks like:
{us:{"2013-01":{"consumer":{"retail":{"pm":{"price":10,"position":1000},"jcp":{"price":15, "position":1500}}}}}}
in time , resource effective way?
something
a[date][sector][industry][ticker].update("pm2" = dict(close=10, position = 1000))
gives me error.
thanks
you havent specified error.
anyways, can done iterating each row , using setdefault method dict, can check item , see if present in dict.
so, example -
a = {} a.setdefault('us', {}) .... .... # if # {'us':{"2013-01":{"consumer":{"retail":{"pm":{"price":10,"position":1000}}}}}} # , iterating on 2nd row a['us']['2013-01']['consumer']['retail'].setupdate('jcp', {}) .....
you may want write function that. idea on how better approach can written.
Comments
Post a Comment