我有一个包含多个词典的列表。每个字典包含7个密钥。但是,键的值是字符串和整数的混合。我想添加整数1的值。
下面是我的词典列表:
我想在所有三个词典中添加vega,theta,delta,gamma的值。
[{'option_type': 'Call', 'expiry': datetime.datetime(1993, 3, 4, 15, 20, 26), 'vega': 2, 'notional': 7840, 'delta': 1, 'strike': 520, 'theta': 5, 'gamma': 3}, {'option_type': 'Call', 'expiry': datetime.datetime(1995, 11, 14, 10, 25, 50), 'vega': 2, 'notional': 1930, 'delta': 1, 'strike': 1960, 'theta': 5, 'gamma': 3}, {'option_type': 'Put', 'expiry': datetime.datetime(1993, 3, 7, 17, 2, 30), 'vega': 2, 'notional': 5530, 'delta': 1, 'strike': 1520, 'theta': 5, 'gamma': 3}]
答案 0 :(得分:2)
这是一个选项:
>>> import datetime
>>> data_set = [{'option_type': 'Call', 'expiry': datetime.datetime(1993, 3, 4, 15, 20, 26), 'vega': 2, 'notional': 7840, 'delta': 1, 'strike': 520, 'theta': 5, 'gamma': 3}, {'option_type': 'Call', 'expiry': datetime.datetime(1995, 11, 14, 10, 25, 50), 'vega': 2, 'notional': 1930, 'delta': 1, 'strike': 1960, 'theta': 5, 'gamma': 3}, {'option_type': 'Put', 'expiry': datetime.datetime(1993, 3, 7, 17, 2, 30), 'vega': 2, 'notional': 5530, 'delta': 1, 'strike': 1520, 'theta': 5, 'gamma': 3}]
>>>
>>> counts = {'vega': 0, 'theta': 0, 'delta': 0, 'gamma': 0}
>>>
>>> for data in data_set:
... for key in counts.keys():
... counts[key] += data[key]
...
>>>
>>> counts
{'gamma': 9, 'vega': 6, 'delta': 3, 'theta': 15}
答案 1 :(得分:0)
如果您已经知道要添加其值的键,则可以在迭代项目并添加它们时创建查找列表。
这些方面的东西:
lookup_count_list = {'vega':0, 'theta':0, 'delta':0, 'gamma':0}
dict_list = [{'option_type': 'Call', 'expiry': datetime.datetime(1993, 3, 4, 15, 20, 26), 'vega': 2, 'notional': 7840, 'delta': 1, 'strike': 520, 'theta': 5, 'gamma': 3}, {'option_type': 'Call', 'expiry': datetime.datetime(1995, 11, 14, 10, 25, 50), 'vega': 2, 'notional': 1930, 'delta': 1, 'strike': 1960, 'theta': 5, 'gamma': 3}, {'option_type': 'Put', 'expiry': datetime.datetime(1993, 3, 7, 17, 2, 30), 'vega': 2, 'notional': 5530, 'delta': 1, 'strike': 1520, 'theta': 5, 'gamma': 3}]
for item in dict_list:
for key, value in item.iteritems():
if key in lookup_count_list.keys():
item[key] += 1
print(lookup_count_list)
答案 2 :(得分:0)
只需迭代并使用https://github.com/josdejong/jsoneditor方法
sum_vega = sum(i['vega'] for i in give_dict)
sum_theta = sum(i['theta'] for i in give_dict)
sum_delta = sum(i['delta'] for i in give_dict)
sum_gamma = sum(i['gamma'] for i in give_dict)
答案 3 :(得分:0)
使用此:
import datetime
my_list=[
{
"vega": 2,
"notional": 7840,
"delta": 1,
"strike": 520,
"theta": 5,
"option_type": "Call",
"gamma": 3,
"expiry": datetime.datetime(1993, 3, 4, 15, 20, 26)
},
{
"vega": 2,
"notional": 1930,
"delta": 1,
"strike": 1960,
"theta": 5,
"option_type": "Call",
"gamma": 3,
"expiry": datetime.datetime(1995, 11, 14, 10, 25, 50)
},
{
"vega": 2,
"notional": 5530,
"delta": 1,
"strike": 1520,
"theta": 5,
"option_type": "Put",
"gamma": 3,
"expiry": datetime.datetime(1993, 3, 7, 17, 2, 30)
}
]
result = {}
for dict_ in my_list:
for key in dict_:
if result.has_key(key):
if isinstance(dict_[key], int):# For integers
result[key] += dict_[key]
#if isinstance(dict_[key], str):# For strings
# result[key] = result[key] + '' + dict_[key]
# We do not sum other types
else:
result[key] = dict_[key]
print result