Python:在字典中汇总元组的产品

时间:2017-10-15 20:22:49

标签: python dictionary

我有一个带有键控元组的字典。我想将每个字典键的2个元组和总和相乘。

我创建了两个类。第一个(ExampleBase)只是创建一个变量和一个字典。第二个(Useful)传递字典并进行计算。

我相信我正在调用第二个类(Useful)中的方法,因为我当前得到的错误是:'ExampleBase'objest没有属性'compute_value'

代码应该这样做:

dictionary = {
    "key1": (v1, v2),
    "key2": (v1, v2),
    "key3": (v1, v2)
}

total = key1v1*key1v2 + key2v1*key2v2 + key3v1*key3v2
class ExampleBase:
    def __init__(self, company_name="N/A", stock_dict={}):
        """
        class constructor
        """
        self.company_name = company_name
        self.stock_dict = stock_dict
        print(self.stock_dict)       
        return


class Useful(ExampleBase):
    """
    Inherits from ExampleBase class
    """
    def __init__(self, company_name, stock_dict):
        super().__init__(name)
        return


    def compute_value(self, stock_dict):
        """
        Computes value of stk
        """
        sum(v1*v2 for v1,v2 in stock_dict.values())   
        return

##
## Program starts running from here
##
if __name__ == "__main__":
    a = {"10-01-2014":(10, 11.25), "10-02-2014":(11, 12.25), "10-03-2014":(12, 13.25)}
    b = ExampleBase("Bern", a)
    b.compute_value(a)

2 个答案:

答案 0 :(得分:0)

这是你要找的吗?

如果您知道每个键的长度值为2:

d = {'a': (1, 2), 'b': (3, 4)}
total = sum(a * b for a, b in d.values())
# total => 10

否则:

import operator
from functools import reduce 
d = {'a': (1, 2), 'b': (3, 4, 5)}
total = sum(reduce(operator.mul, a, 1) for a in d.values())
# total => 62

答案 1 :(得分:0)

我认为你的compute_value功能几乎就在那里。我完成了你的代码。关于错误,因为您从ExampleBase调用该函数。

class ExampleBase:
    def __init__(self, company_name="N/A", stock_dict={}):
        """
        class constructor
        """
        self.company_name = company_name
        self.stock_dict = stock_dict
        print(self.stock_dict)       
        return


class Useful(ExampleBase):
    """
    Inherits from ExampleBase class
    """
        def __init__(self, company_name, stock_dict):
            super().__init__(company_name)
            return


        def compute_value(self, stock_dict):
            """
            Computes value of stk
            """
            val = sum(v1*v2 for v1,v2 in stock_dict.values())   
            return val

    ##
    ## Program starts running from here
    ##
    if __name__ == "__main__":
        a = {"10-01-2014":(10, 11.25), "10-02-2014":(11, 12.25), "10-03-2014":(12, 13.25)}
        b = Useful("Bern", a)
        value = b.compute_value(a)
        print(value)

此外,如果您compute_value的意图是计算创建stock_dict实例时给出的Useful,     你的班级更喜欢这样:

class Useful(ExampleBase):
    """
    Inherits from ExampleBase class
    """
    def __init__(self, company_name, stock_dict):
        super().__init__(company_name, stock_dict)
        return


    def compute_value(self):
        """
        Computes value of stk
        """
        val = sum(v1*v2 for v1,v2 in self.stock_dict.values())   
        return val

使用main

if __name__ == "__main__":
    a = {"10-01-2014":(10, 11.25), "10-02-2014":(11, 12.25), "10-03-2014":(12, 13.25)}
    b = Useful("Bern", a)
    value = b.compute_value()
    print(value)