Python:列表中每个字典的组合

时间:2015-04-01 03:00:42

标签: python dictionary itertools

不确定这是否可行。假设我有一个字典列表,如下所示:

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

然后我有另一个列表,如下:

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

最终结果是将一个不同的(整个)字典附加到weights_list中的每组列表中,如下所示:

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

我希望使用它的原因是我可以调用某些字典键值来乘以分配的相应权重。

我现在拥有的代码,全部由@zehnpaard编写,如下所示:

def portfolios(stocks, weights_list):
    for x in stocks:
        for stock_triplet in itertools.combinations(x, 3):
            for weights in weights_list:
                unique_weight_orders = set(itertools.permutations(weights))
                for weight_order in unique_weight_orders:
                    yield zip(stock_triplet, weight_order)

for port in portfolios(stocks,weights_list):
    print port

然而,这会为每个字典键打印出一个组合,而不是整个字典。我尝试了for x in len(stocks),但它会返回错误'int' object is not iterable,因为很多人可能会假设。{/ p>

提前感谢您收到的任何帮助!

1 个答案:

答案 0 :(得分:1)

我假设您的列表中有两个词典,如下所示:

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

以下代码段,使其打印每个组合:

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

当前输出:

[('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)]

如果你改变它以跳过排列,就像这样:

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

它提供输出:

[('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)]

更改后的完整代码:

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):
    for x in stocks:
        for stock_triplet in itertools.combinations(x, 3):
            for weights in weights_list:
                yield zip(stock_triplet, weights)

for port in portfolios(stocks,weights_list):
    print port