Reputation: 26067
I have a list of stock prices in a dictionary and a list of their values. I want to go through the list to get the value of the stocks on each day but I'm having trouble resolving locations in a list when I'm using a variable to access the data.
Here's a example:
goog = [12,132,124,234,234]
msft = [432,23,234,54,23]
num_days = 5
stocks_i_own = {'goog':50, 'msft':50}
for days in range(0,num_days):
for stock in stocks_i_own:
print days, stock, stocks_i_own[stock]
print stock[days] #prints just 'g' not the value on day0
print 'vaue on ', days, ' is ', stock[days] * stocks_i_own[stock]
The problem is when I use stock[days]
to get the data. I know stock has the correct value('goog' or 'msft') and days has the correct value(range of 0-4), but when using them together I don't get the data from the list but instead the location of on the word itself. It makes sense because something like:
name = 'google'
name[0]
would return g(which its doing in my case) but is there a way for python not to see what I'm providing it as a string but instead a reference to the list?
Upvotes: 3
Views: 1308
Reputation: 15399
Why not store your current lists as a dictionary instead?
Instead of:
goog = [12, 132, 124, 234, 234]
msft = [432, 23, 234, 54, 23]
Use:
stock_vals = { 'goog': [12, 132, 124, 234, 234], 'msft': [432, 23, 234, 54, 23] }
Now you can retrieve them using:
stock_vals[stock_name][day_index]
where stock_name is an element of {'goog', 'msft', ...}
and day_index is an element
of [0, 6]
.
Upvotes: 10
Reputation: 123531
The problem you're having is because you're using a stock's name as both a string's value and as the name of a variable in your script. There are a number of (other) issues with doing things that way, but here's a simple fix:
goog = [12,132,124,234,234]
msft = [432,23,234,54,23]
num_days = 5
stocks_i_own = {'goog':50, 'msft':50}
for day in range(num_days):
for stock in stocks_i_own:
print day, stock, stocks_i_own[stock]
stock_prices = eval(stock) # get list of prices with this stock's name
print stock_prices[day]
print 'value on ', day, ' is ', stock_prices[day] * stocks_i_own[stock]
print
This makes things work by converting the string name of a given stock
string value into the value of a current variable of same name via Python's built-in eval()
function. This function evaluates the expression it's passed in a string argument the same way the Python interpreter would and returns the resulting value. In this case the expression is simply a variable's name, so stock_prices
just becomes another name for either the value of the goog
or the msft
variable.
A slightly better way to do it would be to create another dictionary similar to stocks_i_own
one which holds the list of daily prices for each of the correspondingly named stocks. That way you will be able index each one with same key, the stock's string name held in the stock
variable, to retrieve the corresponding data. Here is what I'm suggesting:
num_days = 5
stocks_i_own = {'goog':50, 'msft':50}
stock_prices = {'goog': [12,132,124,234,234],
'msft': [432,23,234,54,23]}
for day in range(num_days):
for stock in stocks_i_own:
print day, stock, stocks_i_own[stock]
daily_prices = stock_prices[stock]
print daily_prices[day]
print 'value on ', day, ' is ', daily_prices[day] * stocks_i_own[stock]
print
Instead of having the separate daily_prices
variable, the longer expression stock_prices[stock][day]
could be used instead but might not be as understandable.
Upvotes: 1
Reputation: 18438
Just for curiosity's sake:
#!/usr/bin/env python
goog = [12,132,124,234,234]
msft = [432,23,234,54,23]
num_days = 5
stocks_i_own = {'goog':50, 'msft':50}
for days in range(0,num_days):
for stock_name in stocks_i_own: #simmilar stocks_i_own.keys()
globals_bak = globals()
for var_name, var_value in globals_bak.items():
if var_name == stock_name:
studied_stock = var_value
print days, stock_name, studied_stock
print studied_stock[days] #prints just 'g' not the value on day0
print 'vaue on ', days, ' is ', studied_stock[days] * stocks_i_own[stock_name]
I believe this would do what you want, but I would definitely put them in a dictionary!!. (I'm just posting this as an example of what can you do with Python, which doesn't mean that you should do it)
The built in function globals()
returns a dictionary with the global values defined in your current module (check it out: globals)
Also, there's an issue in the part that says:
for stock in stocks_i_own:
#[...]
print stock[days] #prints just 'g' not the value on day0
Is because when you walk a dictionary as for x in dict()
, x
gets the value of the keys (for x in {}
is like saying for x in {}.keys()
) so in your case, you're getting the string "goog" and the string "msft" (not the values contained there) and you're walking the strings.
You're doing more or less this:
>>> a="goog"
>>> for i in range(0,5):
... print a[i]
...
g
o
o
g
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
IndexError: string index out of range
Pyhton is one of the languages where you should definitely listen to uncle Ben...
With great power comes great responsibility
:-)
Upvotes: 1
Reputation: 6404
Actually Stocks_i_own is a dictionary
Use:
for days in range(0,num_days):
for stock, value in stocks_i_own.items():
print days, stock, value
print value[days] #prints just 'g' not the value on day0
print 'vaue on ', days, ' is ', stock[days] * stocks_i_own[stock]
Upvotes: 1
Reputation: 11549
Convert a string to preexisting variable names - you CAN do this, but it's not advised. Better to store goog and msft in a dictionary where the key is a string and the value is a list.
Upvotes: 2
Reputation: 19057
Move the day-by-day values into a dictionary:
stock_values = { 'goog': [12,132,124,234,234],
'msft': [432,23,234,54,23] }
And then look them up like so:
stock_values[ stock ][ days ]
Upvotes: 4