2017-10-15 23 views
-1

我有一個帶有鍵控元組的字典。我想爲每個字典鍵乘以2個元組和總和。Python:在字典裏面加上元組的產品

我創建了兩個類。第一個(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) 
+0

一個具體的例子會讓我們更容易理解! – Elmex80s

+0

'key1v1 * key1v2 + key2v1 * key2v2 + key3v1 * key3v2',這是什麼意思? – Elmex80s

+0

所以對於我的例子(字典a),它會是:sum = 10 * 11.25 + 11 * 12.25 + 12 * 13.25) –

回答

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

我想他想要乘以'x'的值。 – Elmex80s