Reputation: 9
This is my Dictionary
finance={
'sno' : 'None',
'fin_ticker' : 'what',
'marketcap' : ['what','how much'],
'e_value' : ['what', 'how much'],
'ret_on_assets' : ['how much', 'what'],
'tot_cash' : 'what',
'op_cash' :'what',
'lev_free_cf' :'what',
'tot_debt' : 'what',
'curr_ratio' : ['what', 'how much'],
'gross_profit' :['what', 'how much'],
'prof_margin' :['what', 'how much'],
'last_trade' : ['what', 'how much'],
'trade_time' : ['what', 'when'],
'prev_close' : ['what', 'how much'],
}
I need to get keys for the given value..for instance when i gave a value as 'what' it displays all keys regarding to that value as follows
fin_ticker
marketcap
e_value
ret_on_assets
tot_cash
op_cash
lev_free_cf
tot_debt
curr_ratio
gross_profit etc...
Upvotes: 0
Views: 38
Reputation: 757
You should iterate over keys with
for key in finance.keys():
if ( finance[key] == value )
print "key: %s , value: %s" % (key, finance[key])
elif isinstance(finance[key],list) and value in finance[key]:
print "key: %s , value: %s" % (key, finance[key])
Upvotes: 0
Reputation: 107287
You can use a function to return a generator contain the expected keys :
def key_finder(d,val):
for key in d :
value=finance[key]
if value==val:
yield key
elif isinstance(value,list) and val in value:
yield key
Demo :
>>> list(key_finder(finance,'what'))
['op_cash', 'prev_close', 'curr_ratio', 'lev_free_cf', 'last_trade', 'marketcap', 'tot_cash', 'ret_on_assets', 'gross_profit', 'trade_time', 'prof_margin', 'fin_ticker', 'e_value', 'tot_debt']
>>> list(key_finder(finance,'how much'))
['prev_close', 'curr_ratio', 'last_trade', 'marketcap', 'ret_on_assets', 'gross_profit', 'prof_margin', 'e_value']
Upvotes: 1