Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key (/thread-20065.html) |
Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key - firebird - Jul-25-2019 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 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) RE: Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key - firebird - Jul-25-2019 Hello to all! Thank you having taken the time to stop by and review the submitted the problem, I did manage to solve it by changing #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] )to # get Apple's live quote price yahoo_financials = YahooFinancials('AAPL') myQuote = yahoo_financials.get_current_price() listOfKeys = [key for (key, value) in new_dict.items() if value == myQuote] RE: Comaparing Float Values of Dictionary Against A Float Value & Pick Matching Key - scidam - Jul-25-2019 It is worth noting that floats comparison needs additional care when performed. Look at the following example: 1.3 / 3.9 == 13 / 39 This is due to the finite accuracy of representation float numbers in computer memory.As of Python 3.5 (PEP-485), you can do such a comparison using math.isclose function.import math math.isclose(1.3 / 3.9, 13 / 39)
|