gpt4 book ai didi

Python:从列表中元组内的字典中获取字典值并用它做一些事情

转载 作者:太空宇宙 更新时间:2023-11-03 17:48:32 24 4
gpt4 key购买 nike

我碰巧让自己陷入了一个有点复杂的境地(至少对我来说)。

我有一个列表,其中包含一个元组,其中包含来自 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')

感谢收到的任何帮助!

干杯!

最佳答案

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

关于Python:从列表中元组内的字典中获取字典值并用它做一些事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29398597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com