Python: combinations for each dictionary in a list -


not sure if possible or not. assuming have list of dictionaries follows:

stocks = [{'name': 'bob', 'avg_returns': '18.345', 'sd_returns': '2.14', 'var_returns': '34.2334'}, {another_dict}, {another_dict}]

then have list, so:

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)] 

the end result attach different (whole) dictionary each sets of lists within weights_list, demonstrated below:

[({'name': 'bob', 'avg_returns': '18.345', 'sd_returns': '2.14', 'var_returns': '34.2334'}, 0.95),({another_dict}, 0.025), ({another_dict}, 0.025)] 

the reason hoping use can call dictionary key values multiply against respective weight allocated.

the code have now, written @zehnpaard follows:

def portfolios(stocks, weights_list):     x in stocks:         stock_triplet in itertools.combinations(x, 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 

however prints out combination every dictionary key, opposed entire dictionary. tried for x in len(stocks), returns error 'int' object not iterable many of assume.

thanks in advance received!

i have assumed 2 dictionaries within list so:

stocks = [{'name': 'bob', 'avg_returns': '18.345', 'sd_returns': '2.14', 'var_returns': '34.2334'}, { 'abc': 456 }, { 'abc': 123, 98.6: 37 }] 

the following code snippet, causes print every combination:

for weights in weights_list:     unique_weight_orders = set(itertools.permutations(weights))     weight_order in unique_weight_orders:         yield zip(stock_triplet, weight_order) 

current output:

[('sd_returns', 0.95), ('var_returns', 0.025), ('name', 0.025)] [('sd_returns', 0.025), ('var_returns', 0.025), ('name', 0.95)] [('sd_returns', 0.025), ('var_returns', 0.95), ('name', 0.025)] [('sd_returns', 0.9), ('var_returns', 0.05), ('name', 0.05)] [('sd_returns', 0.05), ('var_returns', 0.9), ('name', 0.05)] [('sd_returns', 0.05), ('var_returns', 0.05), ('name', 0.9)] [('sd_returns', 0.075), ('var_returns', 0.075), ('name', 0.85)] [('sd_returns', 0.075), ('var_returns', 0.85), ('name', 0.075)] [('sd_returns', 0.85), ('var_returns', 0.075), ('name', 0.075)] [('sd_returns', 0.1), ('var_returns', 0.1), ('name', 0.8)] [('sd_returns', 0.8), ('var_returns', 0.1), ('name', 0.1)] [('sd_returns', 0.1), ('var_returns', 0.8), ('name', 0.1)] [('sd_returns', 0.75), ('var_returns', 0.125), ('name', 0.125)] [('sd_returns', 0.125), ('var_returns', 0.125), ('name', 0.75)] [('sd_returns', 0.125), ('var_returns', 0.75), ('name', 0.125)] [('sd_returns', 0.7), ('var_returns', 0.15), ('name', 0.15)] [('sd_returns', 0.15), ('var_returns', 0.7), ('name', 0.15)] [('sd_returns', 0.15), ('var_returns', 0.15), ('name', 0.7)] [('sd_returns', 0.95), ('var_returns', 0.025), ('avg_returns', 0.025)] [('sd_returns', 0.025), ('var_returns', 0.025), ('avg_returns', 0.95)] [('sd_returns', 0.025), ('var_returns', 0.95), ('avg_returns', 0.025)] [('sd_returns', 0.9), ('var_returns', 0.05), ('avg_returns', 0.05)] [('sd_returns', 0.05), ('var_returns', 0.9), ('avg_returns', 0.05)] [('sd_returns', 0.05), ('var_returns', 0.05), ('avg_returns', 0.9)] [('sd_returns', 0.075), ('var_returns', 0.075), ('avg_returns', 0.85)] [('sd_returns', 0.075), ('var_returns', 0.85), ('avg_returns', 0.075)] [('sd_returns', 0.85), ('var_returns', 0.075), ('avg_returns', 0.075)] [('sd_returns', 0.1), ('var_returns', 0.1), ('avg_returns', 0.8)] [('sd_returns', 0.8), ('var_returns', 0.1), ('avg_returns', 0.1)] [('sd_returns', 0.1), ('var_returns', 0.8), ('avg_returns', 0.1)] [('sd_returns', 0.75), ('var_returns', 0.125), ('avg_returns', 0.125)] [('sd_returns', 0.125), ('var_returns', 0.125), ('avg_returns', 0.75)] [('sd_returns', 0.125), ('var_returns', 0.75), ('avg_returns', 0.125)] [('sd_returns', 0.7), ('var_returns', 0.15), ('avg_returns', 0.15)] [('sd_returns', 0.15), ('var_returns', 0.7), ('avg_returns', 0.15)] [('sd_returns', 0.15), ('var_returns', 0.15), ('avg_returns', 0.7)] [('sd_returns', 0.95), ('name', 0.025), ('avg_returns', 0.025)] [('sd_returns', 0.025), ('name', 0.025), ('avg_returns', 0.95)] [('sd_returns', 0.025), ('name', 0.95), ('avg_returns', 0.025)] [('sd_returns', 0.9), ('name', 0.05), ('avg_returns', 0.05)] [('sd_returns', 0.05), ('name', 0.9), ('avg_returns', 0.05)] [('sd_returns', 0.05), ('name', 0.05), ('avg_returns', 0.9)] [('sd_returns', 0.075), ('name', 0.075), ('avg_returns', 0.85)] [('sd_returns', 0.075), ('name', 0.85), ('avg_returns', 0.075)] [('sd_returns', 0.85), ('name', 0.075), ('avg_returns', 0.075)] [('sd_returns', 0.1), ('name', 0.1), ('avg_returns', 0.8)] [('sd_returns', 0.8), ('name', 0.1), ('avg_returns', 0.1)] [('sd_returns', 0.1), ('name', 0.8), ('avg_returns', 0.1)] [('sd_returns', 0.75), ('name', 0.125), ('avg_returns', 0.125)] [('sd_returns', 0.125), ('name', 0.125), ('avg_returns', 0.75)] [('sd_returns', 0.125), ('name', 0.75), ('avg_returns', 0.125)] [('sd_returns', 0.7), ('name', 0.15), ('avg_returns', 0.15)] [('sd_returns', 0.15), ('name', 0.7), ('avg_returns', 0.15)] [('sd_returns', 0.15), ('name', 0.15), ('avg_returns', 0.7)] [('var_returns', 0.95), ('name', 0.025), ('avg_returns', 0.025)] [('var_returns', 0.025), ('name', 0.025), ('avg_returns', 0.95)] [('var_returns', 0.025), ('name', 0.95), ('avg_returns', 0.025)] [('var_returns', 0.9), ('name', 0.05), ('avg_returns', 0.05)] [('var_returns', 0.05), ('name', 0.9), ('avg_returns', 0.05)] [('var_returns', 0.05), ('name', 0.05), ('avg_returns', 0.9)] [('var_returns', 0.075), ('name', 0.075), ('avg_returns', 0.85)] [('var_returns', 0.075), ('name', 0.85), ('avg_returns', 0.075)] [('var_returns', 0.85), ('name', 0.075), ('avg_returns', 0.075)] [('var_returns', 0.1), ('name', 0.1), ('avg_returns', 0.8)] [('var_returns', 0.8), ('name', 0.1), ('avg_returns', 0.1)] [('var_returns', 0.1), ('name', 0.8), ('avg_returns', 0.1)] [('var_returns', 0.75), ('name', 0.125), ('avg_returns', 0.125)] [('var_returns', 0.125), ('name', 0.125), ('avg_returns', 0.75)] [('var_returns', 0.125), ('name', 0.75), ('avg_returns', 0.125)] [('var_returns', 0.7), ('name', 0.15), ('avg_returns', 0.15)] [('var_returns', 0.15), ('name', 0.7), ('avg_returns', 0.15)] [('var_returns', 0.15), ('name', 0.15), ('avg_returns', 0.7)] 

if change skip permutations, so:

for weights in weights_list:     yield zip(stock_triplet, weights) 

it gives output:

[('sd_returns', 0.95), ('var_returns', 0.025), ('name', 0.025)] [('sd_returns', 0.9), ('var_returns', 0.05), ('name', 0.05)] [('sd_returns', 0.85), ('var_returns', 0.075), ('name', 0.075)] [('sd_returns', 0.8), ('var_returns', 0.1), ('name', 0.1)] [('sd_returns', 0.75), ('var_returns', 0.125), ('name', 0.125)] [('sd_returns', 0.7), ('var_returns', 0.15), ('name', 0.15)] [('sd_returns', 0.95), ('var_returns', 0.025), ('avg_returns', 0.025)] [('sd_returns', 0.9), ('var_returns', 0.05), ('avg_returns', 0.05)] [('sd_returns', 0.85), ('var_returns', 0.075), ('avg_returns', 0.075)] [('sd_returns', 0.8), ('var_returns', 0.1), ('avg_returns', 0.1)] [('sd_returns', 0.75), ('var_returns', 0.125), ('avg_returns', 0.125)] [('sd_returns', 0.7), ('var_returns', 0.15), ('avg_returns', 0.15)] [('sd_returns', 0.95), ('name', 0.025), ('avg_returns', 0.025)] [('sd_returns', 0.9), ('name', 0.05), ('avg_returns', 0.05)] [('sd_returns', 0.85), ('name', 0.075), ('avg_returns', 0.075)] [('sd_returns', 0.8), ('name', 0.1), ('avg_returns', 0.1)] [('sd_returns', 0.75), ('name', 0.125), ('avg_returns', 0.125)] [('sd_returns', 0.7), ('name', 0.15), ('avg_returns', 0.15)] [('var_returns', 0.95), ('name', 0.025), ('avg_returns', 0.025)] [('var_returns', 0.9), ('name', 0.05), ('avg_returns', 0.05)] [('var_returns', 0.85), ('name', 0.075), ('avg_returns', 0.075)] [('var_returns', 0.8), ('name', 0.1), ('avg_returns', 0.1)] [('var_returns', 0.75), ('name', 0.125), ('avg_returns', 0.125)] [('var_returns', 0.7), ('name', 0.15), ('avg_returns', 0.15)] 

complete code after change:

import itertools  stocks = [{'name': 'bob', 'avg_returns': '18.345', 'sd_returns': '2.14', 'var_returns': '34.2334'}, { 'abc': 456 }, { 'abc': 123, 98.6: 37 }]  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)]  def portfolios(stocks, weights_list):     x in stocks:         stock_triplet in itertools.combinations(x, 3):             weights in weights_list:                 yield zip(stock_triplet, weights)  port in portfolios(stocks,weights_list):     print port 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -