Python: get dictionary value from a dictionary within a tuple within a list and do something with it -


i've happened myself complex situation (for me @ least).

i have list, contains tuple, contains multiple combinations itertools of dictionary , value, presented below:

[({'a':'1,2,3','b':'2','c':'3'},0.25),({'a':'3,4,5','b':'4','c':'6'},0.50),({'a':'10,4,3','b':'6','c':'4'},0.25) 

i need values of keys in order calculation want. lets wanted correlation of 'a' each dictionary 1 other dictionary in tuple:

for tuple in list:     dict in tuple:         = dict.get('a')         a_triplet in intertools.combinations(a, 2):             np.correlate(a,b) a,b in tuple 

i know code egregious many of you, new python (and coding in general) , don't know iterating through tuples , dictionaries.

update

example of input:

[({'url': 'https://ca.finance.yahoo.com/q/hp?s=qec.to&a=02&b=2&c=2005&d=02&e=2&f=2015&g=m&z=66&y=66', 'varreturns': '0.847771901', 'sdreturns': '0.920745296', 'name': 'questerre energy corp (qec.to)', 'avgreturns': '1.292727273'}, 0.25), ({'url': 'https://ca.finance.yahoo.com/q/hp?s=rba.to&a=02&b=2&c=2005&d=02&e=2&f=2015&g=m&z=66&y=66', 'varreturns': '16.6860534', 'sdreturns': '4.084856595', 'name': 'ritchie bros auctioneers inc (rba.to)', 'avgreturns': '20.71140496'}, 0.5), ({'url': 'https://ca.finance.yahoo.com/q/hp?s=rdk.to&a=02&b=2&c=2005&d=02&e=2&f=2015&g=m&z=66&y=66', 'varreturns': '0.038118899', 'sdreturns': '0.195240618', 'name': 'redhawk resources inc (rdk.to)', 'avgreturns': '0.400330579'}, 0.25)] 

each tuple different stock has been assigned different weight through itertools

given code:

import itertools import csv  names = [] stocks = [] open('abctest.csv', 'ru') csvfile:     reader = csv.dictreader(csvfile)     document = reader     row in reader:         stock = row         stocks.append(row)         name = row['name']         names.append(name)  weights_list = [(0.95, 0.025, 0.025),                (0.90, 0.05, 0.05),                (0.85, 0.075, 0.075),                (0.80, 0.1, 0.1),                (0.75, 0.125, 0.125),                (0.70, 0.15, 0.15),                (0.65, 0.175, 0.175),                (0.60, 0.20, 0.20),                (0.55, 0.225, 0.225),                (0.50, 0.25, 0.25)] def portfolios(document, weights_list):     stock_triplet in itertools.combinations(document, 3):         weights in weights_list:             unique_weight_orders = set(itertools.permutations(weights))             weight_order in unique_weight_orders:                 yield zip(stock_triplet, weight_order)  port in portfolios(stocks,weights_list):     print port 

hopeful output example:

10.778966942999999 

given by:

('avgreturns' * weight) + ('avgreturns' * weight) + ('avgreturns' * weight)  #or, taking values given in "example of input"  (0.400330579*0.25)+(20.71140496*0.5)+(1.292727273*0.25) 

as correlation part:

i understand harder, , require use of itertools once again.

let's name each tuple in "example input": "a", "b", , "c". how find correlation between "a" , "b", "b" , "c", , "a", , "c"?

for possible combinations of 'avgreturns' in "example input":

corr_ab = numpy.correlate('avgreturns','avgreturns') corr_ac = numpy.correlate('avgreturns','avgreturns') corr_cb = numpy.correlate('avgreturns','avgreturns') 

thankful recieved!

cheers!

print(sum(float(d['avgreturns'])* b d,b in l)) 

if want a,b a,c , b,c use itertools.combinations:

from itertools import combinations r = [float(d['avgreturns']) * b d, b in l] p = combinations(r,2)  print(sum(r)) 

Comments

Popular posts from this blog

How to run C# code using mono without Xamarin in Android? -

html - grunt SVG to webfont -

c# - SharpSsh Command Execution -