How to add the values of dictionary with same keys in Python - python

I have a list containing multiple dictionaries. Each dictionary contains exact 7 keys. But, the values of keys is mix of string and integer. I want to add the values of integer one.
Below, is my list of dictionaries: I want to just add the values of vega, theta, delta, gamma from all the three dictionaries.
[{'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}]

Here is one option:
>>> 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}

Simply iterate and use sum() method
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)

Use this:
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

Well if you already know the keys whose values you want to add, you can simply create a lookup list while iterating through the items and adding them.
Something along these lines:
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)

Related

Sort a Python dictionary within a dictionary

I am trying to sort a dictionary within a dictionary. My goal is to sort the 'sub' dictionary ['extra'] based on it's values, from high to low.The problem I'm having is that my 'sub' dictionary is nested deep within the main dictionary. Using other examples, I can do this for one level higher, see my code below. So instead of sorting 'marks', I would like to sort the items 1,2 & 3 based on their values. Code:
# initializing dictionary
test_dict = {'Nikhil' : { 'roll' : 24, 'marks' : 17, 'extra' : {'item1': 2, 'item2': 3, 'item3': 5}},
'Akshat' : {'roll' : 54, 'marks' : 12, 'extra' : {'item1': 8, 'item2': 3, 'item3': 4}},
'Akash' : { 'roll' : 12, 'marks' : 15, 'extra' : {'item1': 9, 'item2': 3, 'item3': 1}}}
# printing original dict
print("The original dictionary : " + str(test_dict))
# using sorted()
# Sort nested dictionary by key
res = sorted(test_dict.items(), key = lambda x: x[1]['marks'])
# print result
print("The sorted dictionary by marks is : " + str(res))
# How to sort on 'extra'?
So this is what I want it to look like:
sorted_dict = {'Nikhil' : { 'roll' : 24, 'marks' : 17, 'extra' : {'item3': 5, 'item2': 3, 'item1': 2}},
'Akshat' : {'roll' : 54, 'marks' : 12, 'extra' : {'item1': 8, 'item3': 4, 'item2': 3}},
'Akash' : { 'roll' : 12, 'marks' : 15, 'extra' : {'item1': 9, 'item2': 3, 'item3': 1}}}
Well this seems to do it:
test_dict = {
'Nikhil': {'roll': 24, 'marks': 17, 'extra': {'item1': 2, 'item2': 3, 'item3': 5}},
'Akshat': {'roll': 54, 'marks': 12, 'extra': {'item1': 8, 'item2': 3, 'item3': 4}},
'Akash': {'roll': 12, 'marks': 15, 'extra': {'item1': 9, 'item2': 3, 'item3': 1}}
}
sorted_dict = test_dict.copy()
for name in test_dict:
extra = test_dict[name]['extra']
sorted_extra = dict(reversed(sorted(extra.items(), key=lambda item: item[1])))
sorted_dict[name]['extra'] = sorted_extra
this sorts the values but not the keys of the 'extra' dict for all dicts in the big dictionary:
test_dict = {
'Nikhil': {'roll': 24, 'marks': 17, 'extra': {'item1': 2, 'item2': 3, 'item3': 5}},
'Akshat': {'roll': 54, 'marks': 12, 'extra': {'item1': 8, 'item2': 3, 'item3': 4}},
'Akash': {'roll': 12, 'marks': 15, 'extra': {'item1': 9, 'item2': 3, 'item3': 1}}
}
for dct in test_dict.values():
extra_keys, extra_values = dct['extra'].keys(), dct['extra'].values()
dct['extra'] = dict(zip(extra_keys, sorted(extra_values, reverse=True)))
print(test_dict)
output:
{'Nikhil': {'roll': 24, 'marks': 17, 'extra': {'item1': 5, 'item2': 3, 'item3': 2}},
'Akshat': {'roll': 54, 'marks': 12, 'extra': {'item1': 8, 'item2': 4, 'item3': 3}},
'Akash': {'roll': 12, 'marks': 15, 'extra': {'item1': 9, 'item2': 3, 'item3': 1}}}
Same thing can also be achived by List Comprehension
test_dict = {
'Nikhil': {'roll': 24, 'marks': 17, 'extra': {'item1': 2, 'item2': 3, 'item3': 5}},
'Akshat': {'roll': 54, 'marks': 12, 'extra': {'item1': 8, 'item2': 3, 'item3': 4}},
'Akash': {'roll': 12, 'marks': 15, 'extra': {'item1': 9, 'item2': 3, 'item3': 1}}
}
result = {
k: {
m: dict(sorted(n.items(), reverse=True, key=lambda x: x[1]))
if m == 'extra' else n
for (m, n) in v.items()
} for (k, v) in test_dict.items()
}
print(result)
sort the inner dict and assign it to test_dict[key]['extra'].. just with a loop
for key in test_dict.keys():
test_dict[key]["extra"] = dict(sorted(test_dict[key]["extra"].items(), key=itemgetter(1), reverse=True))
than the test_dict output would be as
{
'Nikhil': {'roll': 24, 'marks': 17, 'extra': {'item3': 5, 'item2': 3, 'item1': 2}},
'Akshat': {'roll': 54, 'marks': 12, 'extra': {'item1': 8, 'item3': 4, 'item2': 3}},
'Akash': {'roll': 12, 'marks': 15, 'extra': {'item1': 9, 'item2': 3, 'item3': 1}}
}

How to change the color of bars in a bar graph according to its x ticks? (Matplotlib, Python)

I want to change the bar color of the state: AZ, CA, FL, NY, OH, and OK. I did it by counting the index; however, I am wondering if I can change the color according to the names of the x ticks.
import matplotlib.pylab as plt
fig=plt.figure(figsize=(10,8), dpi= 90)
lists = sorted(frequency_state.items())
x, y = zip(*lists)
bars = plt.bar(x, y, color = 'grey')
plt.grid()
plt.xticks(rotation = 90)
for i in [2,3,5,23,24,25,31]:
bars[i].set_color('r')
plt.show()
{'FL': 45,
'OK': 37,
'OH': 33,
'NY': 28,
'TX': 27,
'CA': 25,
'AZ': 17,
'GA': 10,
'KY': 9,
'MN': 8,
'MA': 8,
'LA': 8,
'PA': 7,
'ID': 7,
'NJ': 6,
'VA': 6,
'IN': 6,
'MT': 6,
'TN': 5,
'CT': 5,
'NC': 5,
'WI': 5,
'MD': 4,
'IL': 4,
'UT': 3,
'IA': 3,
'MI': 3,
'AR': 2,
'MO': 2,
'SC': 2,
'AL': 2,
'NV': 2,
'OR': 1,
'SD': 1,
'ND': 1}
Here is the graph:
Normalize the value in the colormap you want to display and set it to the desired color of the bar chart.
import matplotlib.pylab as plt
import matplotlib.colors as mcolors
frequency_state = {'FL': 45, 'OK': 37, 'OH': 33, 'NY': 28, 'TX': 27, 'CA': 25, 'AZ': 17, 'GA': 10, 'KY': 9, 'MN': 8,
'MA': 8, 'LA': 8, 'PA': 7, 'ID': 7, 'NJ': 6, 'VA': 6, 'IN': 6, 'MT': 6, 'TN': 5, 'CT': 5, 'NC': 5, 'WI': 5,
'MD': 4, 'IL': 4, 'UT': 3, 'IA': 3, 'MI': 3, 'AR': 2, 'MO': 2, 'SC': 2, 'AL': 2, 'NV': 2, 'OR': 1, 'SD': 1, 'ND': 1}
fig=plt.figure(figsize=(10,8), dpi= 90)
ax = plt.subplot()
colormap = plt.cm.Blues
normalize = mcolors.Normalize(vmin=min(frequency_state.values()), vmax=max(frequency_state.values()))
lists = sorted(frequency_state.items())
x, y = zip(*lists)
bars = plt.bar(x, y, color='grey')
plt.grid()
plt.xticks(rotation = 90)
for i in [2,3,5,23,24,25,31]:
bars[i].set_color(colormap(normalize(lists[i][1])))
plt.show()

How to filter and remove dict elements based on a value threshold?

I have several dictionaries in a list with this structure:
[{'store': 'walmart',
'store_id': 0,
'store_info': {'grapes': {'availability': {'No': 1, 'Yes': 1}},
'tomatoes': {'availability': {'No': 5, 'Yes': 6}},
'oranges': {'availability': {'No': 2, 'Yes': 2}},
'bottled water': {'availability': {'No': 10, 'Yes': 5}},
"india's mangos": {'availability': {'No': 3, 'Yes': 5}},
'water melon': {'availability': {'No': 2, 'Yes': 2}},
'lemons': {'availability': {'No': 2, 'Yes': 3}},
'kiwifruit': {'availability': {'No': 4, 'Yes': 2}},
'pineapple': {'availability': {'No': 5, 'Yes': 20}},
'total_yes': 23,
'total_no': 23,
'total': 46,
'id': [3, 4, 6, 2, 1, 6, 1, 4, 2]}},
{'store': 'Costco',
'store_id': 24,
'store_info': {'papaya': {'availability': {'No': 1, 'Yes': 1}},
'lychee': {'availability': {'No': 5, 'Yes': 1}},
'fig': {'availability': {'No': 2, 'Yes': 2}},
'blackberry': {'availability': {'No': 2, 'Yes': 5}},
"india's mangos": {'availability': {'No': 3, 'Yes': 5}},
'plum': {'availability': {'No': 1, 'Yes': 2}},
'total_yes': 43,
'total_no': 3,
'total': 46,
'id': [3, 4, 36, 2, 1, 1, 2, 4, 2]}}
]
How can I filter all the Yes and No values which are greater or equal to 5 at the same time? For example, given the above dict. The expected output should look like this if the dictionary fullfil the condition:
[
{'store': 'walmart',
'store_id': 0,
'store_info': {
'tomatoes': {'availability': {'No': 5, 'Yes': 6}},
'bottled water': {'availability': {'No': 10, 'Yes': 5}},
'pineapple': {'availability': {'No': 5, 'Yes': 20}},
'total_yes': 23,
'total_no': 23,
'total': 46,
'id': [3, 4, 6, 2, 1, 6, 1, 4, 2]}
}
]
In the above example, 'india's mangos': {'availability': {'No': 3, 'Yes': 5}} should be filtered or removed. Because, although the 5 fullfil Yes treshold, the key No, doesnt fulfill the treshold at the same time. Alternatively, 'pineapple': {'availability': {'No': 5, 'Yes': 20}}, should remain in the dict, because Yes key has as values 20, which is greater than the 5 threshold. Finally, the second dict (costco) should be removed because none of its keys are at leas 5.
So far I tried to iterate over the structure, however, I am making too many loops, is there a more compact way of getting the expected output?:
a_lis = []
for e in list_dict:
try:
l = list(e['store_info'].keys())
for i in l:
#print(e['store_info'][i]['availability'])
if e['store_info'][i]['availability']['No']>=5 and e['availability'][i]['availability']['Yes']>= 5:
a_lis.append(e['store_info'][i]['availability'])
print(a_lis)
else:
pass
except TypeError:
pass
That's not difficult.I would recommend you create a new list.(And revise the dictionary directly.)
lst = [{'store': 'walmart',
'store_id': 0,
'store_info': {'grapes': {'availability': {'No': 1, 'Yes': 1}},
'tomatoes': {'availability': {'No': 5, 'Yes': 6}},
'oranges': {'availability': {'No': 2, 'Yes': 2}},
'bottled water': {'availability': {'No': 10, 'Yes': 5}},
'india\'s mangos': {'availability': {'No': 3, 'Yes': 5}},
'water melon': {'availability': {'No': 2, 'Yes': 2}},
'lemons': {'availability': {'No': 2, 'Yes': 3}},
'kiwifruit': {'availability': {'No': 4, 'Yes': 2}},
'pineapple': {'availability': {'No': 5, 'Yes': 20}},
'total_yes': 23,
'total_no': 23,
'total': 46,
'id': [3, 4, 6, 2, 1, 6, 1, 4, 2]}},
{'store': 'Costco',
'store_id': 24,
'store_info': {
'papaya': {'availability': {'No': 1, 'Yes': 1}},
'lychee': {'availability': {'No': 5, 'Yes': 1}},
'fig': {'availability': {'No': 2, 'Yes': 2}},
'blackberry': {'availability': {'No': 2, 'Yes': 5}},
'india\'s mangos': {'availability': {'No': 3, 'Yes': 5}},
'plum': {'availability': {'No': 1, 'Yes': 2}},
'total_yes': 43,
'total_no': 3,
'total': 46,
'id': [3, 4, 36, 2, 1, 1, 2, 4, 2]}}
]
result_list = []
for sub_dict in lst:
if sub_dict['store_info']['total_yes'] >= 5 and sub_dict['store_info']['total_no'] >= 5:
result_list.append(sub_dict)
key_need_to_be_removed = [k for k, v in sub_dict['store_info'].items() if type(v) is dict and (v['availability']['Yes'] < 5 or v['availability']['No'] < 5)]
for k in key_need_to_be_removed: # remove the dict under dictionary['store_info']
del sub_dict['store_info'][k]
print(result_list)
Result:
[{
'store': 'walmart',
'store_id': 0,
'store_info': {
'tomatoes': {
'availability': {
'No': 5,
'Yes': 6
}
},
'bottled water': {
'availability': {
'No': 10,
'Yes': 5
}
},
'pineapple': {
'availability': {
'No': 5,
'Yes': 20
}
},
'total_yes': 23,
'total_no': 23,
'total': 46,
'id': [3, 4, 6, 2, 1, 6, 1, 4, 2]
}
}]
Here is another approach:
# where data is the input
filtered = []
for store in data:
avail_dict = {}
extra_dict = {}
for item, value in store['store_info'].items():
if isinstance(value, dict):
okay = value['availability'].get('No',0) >= 5 and value['availability'].get('Yes',0) >= 5
if okay:
avail_dict[item] = value
else:
extra_dict[item] = value
if avail_dict:
avail_dict.update(extra_dict)
new_store = dict(store)
new_store['store_info'] = avail_dict
filtered.append(new_store)
Result for filtered (input data is unchanged):
[{'store': 'walmart',
'store_id': 0,
'store_info': {'tomatoes': {'availability': {'No': 5, 'Yes': 6}},
'bottled water': {'availability': {'No': 10, 'Yes': 5}},
'pineapple': {'availability': {'No': 5, 'Yes': 20}},
'total_yes': 23,
'total_no': 23,
'total': 46,
'id': [3, 4, 6, 2, 1, 6, 1, 4, 2]}}]

how to calculate the average disk_available, based on the hostname hourly , python

result data :
<QuerySet [{'disk_available': 26, 'hostname': '2', 'day': datetime.datetime(2020, 2, 11, 0, 0, tzinfo=<UTC>), 'c': 354}, {'disk_available': 27, 'hostname': '2', 'day': datetime.datetime(2020, 2, 10, 0, 0, tzinfo=<UTC>), 'c': 273}, {'disk_available': 19, 'hostname': '2', 'day': datetime.datetime(2020, 2, 12, 0, 0, tzinfo=<UTC>), 'c': 12}, {'disk_available': 26, 'hostname': '2', 'day': datetime.datetime(2020, 2, 12, 0, 0, tzinfo=<UTC>), 'c': 45}, {'disk_available': 26, 'hostname': 'tes', 'day': datetime.datetime(2020, 2, 11, 0, 0, tzinfo=<UTC>), 'c': 1945}, {'disk_available': 19, 'hostname': 'tes', 'day': datetime.datetime(2020, 2, 12, 0, 0, tzinfo=<UTC>), 'c': 53}, {'disk_available': 1, 'hostname': 'tes', 'day': datetime.datetime(2020, 2, 11, 0, 0, tzinfo=<UTC>), 'c': 1}, {'disk_available': 26, 'hostname': 'tes', 'day': datetime.datetime(2020, 2, 12, 0, 0, tzinfo=<UTC>), 'c': 45}, {'disk_available': 27, 'hostname': 'tes', 'day': datetime.datetime(2020, 2, 10, 0, 0, tzinfo=<UTC>), 'c': 291}]>
SocketClient.objects.annotate(day=TruncDay('create')).values('day').annotate(c=Count('id')).values('day', 'disk_available', 'hostname', 'c').order_by('hostname')
.
from the results above I want to do that , I want to display the average result of hostname hourly

Sort list of dictionaries based on nested keys

[{'AvailableOffers': (d7b000a:Order {Amount:1000,Name:"000091",OfferedC:"JD",SeekingC:"Taobao",UserName:"xalima",ValidTill:"2019-11-30"}), 'Participants': 2, 'OrderID': ['000089', '000091', '000089']}
{'AvailableOffers': (d7b000a:Order {Amount:1000,Name:"000091",OfferedC:"JD",SeekingC:"Taobao",UserName:"xalima",ValidTill:"2019-11-30"}), 'Participants': 2, 'OrderID': ['000089', '000091', '000089']}
{'AvailableOffers': (b222004:Order {Amount:1000,Name:"000093",OfferedC:"JD",SeekingC:"China Airline",UserName:"yunis",ValidTill:"2017-11-11"}), 'Participants': 3, 'OrderID': ['000089', '000093', '000090', '000089']}
{'AvailableOffers': (d7b000a:Order {Amount:1000,Name:"000091",OfferedC:"JD",SeekingC:"Taobao",UserName:"xalima",ValidTill:"2019-11-30"}), 'Participants': 5, 'OrderID': ['000089', '000091', '000096', '000095', '000090', '000089']}
{'AvailableOffers': (d7b000a:Order {Amount:1000,Name:"000091",OfferedC:"JD",SeekingC:"Taobao",UserName:"xalima",ValidTill:"2019-11-30"}), 'Participants': 6, 'OrderID': ['000089', '000091', '000096', '000097', '000093', '000090', '000089']}]
That is the list dictionary I want to sort, what i can do now is to sort
ListData_by_Participants = sorted(ListData, key=itemgetter("Participants"))
What i want to get help is
ListData_by_Validity = sorted(ListData,
key=itemgetter("AvailableOffers")("ValidTill"))
is there a way to manage this ?
In order to sort list of dictionary with nested keys, you may use lambda expression as:
my_dict_list = [{'parent_key': {'my_key_1': 10, 'my_key_2': 2}},
{'parent_key': {'my_key_1': 5, 'my_key_2': 4}},
{'parent_key': {'my_key_1': 10, 'my_key_2': 6}},
{'parent_key': {'my_key_1': 5, 'my_key_2': 2}},
{'parent_key': {'my_key_1': 10, 'my_key_2': 3}},
]
sorted(my_dict_list, key=lambda x: (
x['parent_key']['my_key_1'], x['parent_key']['my_key_2']))
# value of first 'key' to sort-^ ^
# in case of same value for first 'key'-^
# sort based on second key -------------^
which returns the sorted list as:
[{'parent_key': {'my_key_2': 2, 'my_key_1': 5}},
{'parent_key': {'my_key_2': 4, 'my_key_1': 5}},
{'parent_key': {'my_key_2': 2, 'my_key_1': 10}},
{'parent_key': {'my_key_2': 3, 'my_key_1': 10}},
{'parent_key': {'my_key_2': 6, 'my_key_1': 10}}]

Categories