I am trying to create a portfolio class. I want to zip my two lists into a dictionary but when ai try to print it out, it is empty(even though my lists are not). Am i missing something??
import numpy as np
class Portfolio(object):
"""description of class"""
array_of_stock_prices=[]
array_of_stock_names=[]
#create a dictionary of stocks
stocks=dict(zip(array_of_stock_names,array_of_stock_prices))
def __init__(self):
print()
def AddStock(self,stock_ticker,stock_price):
self.array_of_stock_names.append(stock_ticker)
self.array_of_stock_prices.append(stock_price)
def printObject(self):
for key,value in self.stocks.items():
print(key,value)
port1=Portfolio()
port1.AddStock('AApl',100)
port1.printObject()
You create the dictionary only once, with empty lists. If you want to keep it up to date, put that line of code after every extension of the lists (in the end of AddStock
), and keep this dictionary as an attribute of self
, like
self.stocks = dict(zip(self.array_of_stock_names, self.array_of_stock_prices))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.