Jul-25-2019, 07:10 PM
Hello Everyone!
Would you help me with the following problem. I have a function that pull data from yahoo finance and return a dictionary of stock names and current stock values - floats. I subsequently use that dictionary in another function, using a float value (pulled from the market as well, e.i getKeysByValues(new_dict, myQuote )) i want to find in my dictionary which key correspond to that value, e.i. myQuote.
I get this
This is the full script:
Would you help me with the following problem. I have a function that pull data from yahoo finance and return a dictionary of stock names and current stock values - floats. I subsequently use that dictionary in another function, using a float value (pulled from the market as well, e.i getKeysByValues(new_dict, myQuote )) i want to find in my dictionary which key correspond to that value, e.i. myQuote.
I get this
Error:Traceback (most recent call last):
File "C:\Users\...\Desktop\testProcess1.py", line 43, in <module>
listOfKeys = getKeysByValues(new_dict, myQuote )
File "C:\Users\...\Desktop\testProcess1.py", line 33, in getKeysByValues
if item[1] in listOfValues:
TypeError: argument of type 'numpy.float64' is not iterable
error when i use # get Apple's live quote price myQuote = live.get_live_price("AAPL")#resulting value here is a float listOfKeys = getKeysByValues(new_dict, myQuote )#note myQuote has no square bracket #Iterate over the list of values for key in listOfKeys:But when i use
# get Apple's live quote price myQuote = live.get_live_price("AAPL")#resulting value here is a float listOfKeys = getKeysByValues(new_dict, [myQuote] )#myQuote has square bracket #Iterate over the list of values for key in listOfKeys: print(key)the scrpt runs but produces nothing
This is the full script:
#import all modules from yahoo_fin import stock_info as live from yahoofinancials import YahooFinancials import pandas as pd import numpy as np #Stock stickers to get data - declared globally stocks = ['AAPL','MSFT','TSLA'] #Function to extract data def getStockData(): #Construct yahoo financials objects for data extraction yahoo_financials = YahooFinancials(stocks) price = yahoo_financials.get_current_price() Open = yahoo_financials.get_open_price() High = yahoo_financials.get_daily_high() Low = yahoo_financials.get_daily_low() perChange = yahoo_financials.get_current_percent_change() Volume = yahoo_financials.get_current_volume() return(price, Open, High, Low, perChange, Volume) getChg = lambda: getStockData()[0] #getChg = getStockData()[4], output:{'AAPL': 208.21, 'MSFT': 140.25, 'TSLA': 227.03} new_dict = {} new_dict.update(getChg())# #dictionary of string and floats - data to compare against ''' Get a list of keys from dictionary which has value that matches with any value in given list of values ''' def getKeysByValues(dictOfElements, listOfValues): listOfKeys = list() listOfItems = dictOfElements.items() for item in listOfItems: if item[1] in listOfValues: listOfKeys.append(item[0]) return listOfKeys #Now lets use this to find all the keys from dictionary whose values is equal to any value from the list i.e ''' Get list of keys with any of the given values ''' #test market value to find match with # get Apple's live quote price myQuote = live.get_live_price("AAPL")#resulting value here is a float listOfKeys = getKeysByValues(new_dict, [myQuote] ) #Iterate over the list of values for key in listOfKeys: print(key)