Python:从列表中的元组中的字典中获取字典值并对其进行处理
Python: get dictionary value from a dictionary within a tuple within a list and do something with it
我碰巧遇到了一个有点复杂的情况(至少对我而言)。
我有一个列表,其中包含一个元组,其中包含字典 itertools
中的多个 combinations
和一个值,如下所示:
[({'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')
感谢收到的任何帮助!
干杯!
products = [int(my_dict.get('a',0))*b for my_dict,b in my_list]
可能是您想要...?
print(sum(float(d['avgreturns'])* b for d,b in l))
如果你想要 a,b a,c 和 b,c 使用 itertools.combinations
:
from itertools import combinations
r = [float(d['avgreturns']) * b for d, b in l]
p = combinations(r,2)
print(sum(r))
我碰巧遇到了一个有点复杂的情况(至少对我而言)。
我有一个列表,其中包含一个元组,其中包含字典 itertools
中的多个 combinations
和一个值,如下所示:
[({'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')
感谢收到的任何帮助!
干杯!
products = [int(my_dict.get('a',0))*b for my_dict,b in my_list]
可能是您想要...?
print(sum(float(d['avgreturns'])* b for d,b in l))
如果你想要 a,b a,c 和 b,c 使用 itertools.combinations
:
from itertools import combinations
r = [float(d['avgreturns']) * b for d, b in l]
p = combinations(r,2)
print(sum(r))