Oct-15-2017, 07:48 PM
I'm having trouble figuring out how to implement this.
I have a dictionary with keyed touples. I would like to multiply the 2 touples and sum for every dictionary key.
I've created two classes. The first (ExampleBase) simply appends entries to the dictionary. The second (Useful) passes the appended dictionary and does the calculation.
I know I want a for loop to sum for all dictionary keys. I'm not sure how to access the touples within the dictionary to multiply them.
The code should do this: dictionary = {key1:(v1, v2), key2:(v1, v2), key3:(V1, v2)}
total = key1v1*key1v2 + key2v1*key2v2 + key3v1*key3v2
I have a dictionary with keyed touples. I would like to multiply the 2 touples and sum for every dictionary key.
I've created two classes. The first (ExampleBase) simply appends entries to the dictionary. The second (Useful) passes the appended dictionary and does the calculation.
I know I want a for loop to sum for all dictionary keys. I'm not sure how to access the touples within the dictionary to multiply them.
The code should do this: dictionary = {key1:(v1, v2), key2:(v1, v2), key3:(V1, v2)}
total = key1v1*key1v2 + key2v1*key2v2 + key3v1*key3v2
class ExampleBase: """ This is the ExampleBase class """ 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 def __str__(self): """ Prints the company name string """ str = "The Company name is: %s" %\ (self.company_name ) return str def add_purchase(self, addtlSTK): """ Adds item to stock_dict """ self.stock_dict.update(addtlSTK) 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 """ stk_key=len(stock_dict) print(stk_key) for k, v in stock_dict(): total = 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) c = {"10-04-2014":(13, 14.25)} b.add_purchase(c) print(b) b.compute_value