Python 在DICT列表中搜索大于给定值的值?
因此,我有一份清单,其中列出了4种不同的潜在期权交易的不同执行价、买入价和隐含波动率Python 在DICT列表中搜索大于给定值的值?,python,list,dictionary,finance,computational-finance,Python,List,Dictionary,Finance,Computational Finance,因此,我有一份清单,其中列出了4种不同的潜在期权交易的不同执行价、买入价和隐含波动率 search_option = [{'strike_price': '1', 'bid_price': '0.25', 'implied_volatility': '0.94' }, {'strike_price': '3.5', 'bid_price': '0.20', 'implied_volatility': '0.88'}, {'strike_price': '2', 'bid_price': '0.
search_option = [{'strike_price': '1', 'bid_price': '0.25', 'implied_volatility': '0.94' },
{'strike_price': '3.5', 'bid_price': '0.20', 'implied_volatility': '0.88'},
{'strike_price': '2', 'bid_price': '0.05', 'implied_volatility': None},
{'strike_price': '3.5', 'bid_price': '0.31', 'implied_volatility': '0.25'}]
在这里,代码搜索隐含波动率最高的期权,并给我一个输出
highest_IV, highest_idx = 0, None
for idx, option in enumerate(search_option):
if option['implied_volatility'] and highest_IV < float(option['implied_volatility']):
highest_IV = float(option['implied_volatility'])
highest_idx = idx
if highest_idx is not None:
print("Strike Price: {strike_price}, Bid: {bid_price}, IV: {implied_volatility}".format(**search_option[highest_idx]))
我真的不再需要“最高隐含波动率”代码了
我现在的任务是,如何搜索执行价>3且出价<0.25的期权
然后,我需要将匹配字典的所有关键字strike、bid、Implicated_volatility保存为单独的变量,就像我对上述变量所做的那样。尝试以下操作:
search_option = [{'strike_price': '1', 'bid_price': '0.25', 'implied_volatility': '0.94' },
{'strike_price': '3.5', 'bid_price': '0.20', 'implied_volatility': '0.88'},
{'strike_price': '2', 'bid_price': '0.05', 'implied_volatility': None},
{'strike_price': '3.5', 'bid_price': '0.31', 'implied_volatility': '0.25'}]
sort = [i for i in search_option if float(i["strike_price"]) > 3 and float(i["bid_price"]) < 0.25]
print(sort)
问题,你能演示一下如何将每个键保存为单独的变量吗?类似这样的?x={'strike_price':'3.5','bid_price':'0.20','隐含波动率':'0.88'}strike=x[strike_price]bid=x[bid_price]vol=x[隐含波动率]是的。但是有没有可能我可以把它们保存为浮点数而不是字符串?是的。您可以使用float函数来实现这一点。您可以使用strike=floatx[strike\u price]代替strike=x[strike\u price]。这里有一个有用的链接来解释
search_option = [{'strike_price': '1', 'bid_price': '0.25', 'implied_volatility': '0.94' },
{'strike_price': '3.5', 'bid_price': '0.20', 'implied_volatility': '0.88'},
{'strike_price': '2', 'bid_price': '0.05', 'implied_volatility': None},
{'strike_price': '3.5', 'bid_price': '0.31', 'implied_volatility': '0.25'}]
sort = [i for i in search_option if float(i["strike_price"]) > 3 and float(i["bid_price"]) < 0.25]
print(sort)
[{'strike_price': '3.5', 'bid_price': '0.20', 'implied_volatility': '0.88'}]