Python:从列表中的元组内的字典中获取字典值,并对其执行某些操作

时间:2015-04-01 18:54:37

标签: python list dictionary tuples

我碰巧让自己陷入了一种复杂的情况(对我来说至少)。

我有一个列表,其中包含一个元组,其中包含来自字典的combinations的多个itertools和一个值,如下所示:

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

我需要获取某些键的值才能进行我想要的计算。让我们说我希望得到每个字典的'a'与元组中的另一个字典的相关性:

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

我知道这段代码对你们很多人来说可能都很不可思议,但我是python(以及一般编码)的新手,并且对迭代元组和词典不太了解。

更新

输入示例:

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

每个元组都是通过itertools分配不同权重的不同股票

由代码给出:

import itertools
import csv

names = []
stocks = []
with open('AbCtest.csv', 'rU') as csvfile:
    reader = csv.DictReader(csvfile)
    document = reader
    for 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):
    for stock_triplet in itertools.combinations(document, 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

有希望的输出示例:

10.778966942999999

鉴于:

('avgreturns' * weight) + ('avgreturns' * weight) + ('avgreturns' * weight)

#or, from taking the values given in "example of input"

(0.400330579*0.25)+(20.71140496*0.5)+(1.292727273*0.25)

关于相关部分:

我知道这会更难,并且可能需要再次使用itertools

假设我们在“示例输入”中命名每个元组:“a”,“b”和“c”。我们如何找到“a”和“b”,“b”和“c”,“a”和“c”之间的相关性?

“示例输入”中“avgreturns”的所有可能组合:

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

感谢收到的任何帮助!

干杯!

2 个答案:

答案 0 :(得分:1)

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

如果您想要a,b a,c和b,请使用itertools.combinations

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

答案 1 :(得分:0)

products = [int(my_dict.get('a',0))*b for my_dict,b in my_list]

可能是你想要的......?