Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - 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: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV (/thread-18703.html) Pages:
1
2
|
Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - dn237 - May-28-2019 Bittrex_Markets = requests.get("https://api.bittrex.com/api/v1.1/public/getmarkets").json() print (Bittrex_Markets) >>> Editing post it was way too long first time RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - heiner55 - May-28-2019 #!/usr/bin/python3 import requests, pprint url = "https://api.bittrex.com/api/v1.1/public/getmarkets" rsp = requests.get(url) if rsp.status_code == 200: json_data = rsp.json() pprint.pprint(json_data) else: print("ERROR:", rsp.status_code) print(rsp.text)
RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - dn237 - May-28-2019 Bittrex_Markets = requests.get("https://api.bittrex.com/api/v1.1/public/getmarkets").json() Print (Bittrex_Markets) >>> {'success': True, 'message': '', 'result': [{'MarketCurrency': 'LTC', 'BaseCurrency': 'BTC', 'MarketCurrencyLong': 'Litecoin', 'BaseCurrencyLong': 'Bitcoin', 'MinTradeSize': 0.01686767, 'MarketName': 'BTC-LTC', 'IsActive': True, 'IsRestricted': False, 'Created': '2014-02-13T00:00:00', 'Notice': None, 'IsSponsored': None, 'LogoUrl': 'https://bittrexblobstorage.blob.core.windows.net/public/6defbc41-582d-47a6-bb2e-d0fa88663524.png'}, {'MarketCurrency': 'DOGE', 'BaseCurrency': 'BTC', 'MarketCurrencyLong': 'Dogecoin', 'BaseCurrencyLong': 'Bitcoin', 'MinTradeSize': 480.76923077, 'MarketName': 'BTC-DOGE', 'IsActive': True, 'IsRestricted': False, 'Created': '2014-02-13T00:00:00', 'Notice': None, 'IsSponsored': None, 'LogoUrl': 'https://bittrexblobstorage.blob.core.windows.net/public/a2b8eaee-2905-4478-a7a0-246f212c64c6.png'}, {'MarketCurrency': 'VDX', 'BaseCurrency': 'ETH', 'MarketCurrencyLong': 'Vodi X', 'BaseCurrencyLong': 'Ethereum', 'MinTradeSize': 300.0, 'MarketName': 'ETH-VDX', 'IsActive': True, 'IsRestricted': True, 'Created': '2019-05-23T00:41:32.987', 'Notice': None, 'IsSponsored': None, 'LogoUrl': 'https://bittrexblobstorage.blob.core.windows.net/public/3277e3cb-fe62-46df-9162-3d9ba67c521e.png'}]} Bittrex_Get_Market_Summaries = requests.get("https://api.bittrex.com/api/v1.1/public/getmarketsummaries").json() Print (Bittrex_Get_Market_Summaries) >>> {'success': True, 'message': '', 'result': [{'MarketName': 'BTC-DYN', 'High': 1.657e-05, 'Low': 1.437e-05, 'Volume': 83445.47466841, 'Last': 1.468e-05, 'BaseVolume': 1.27935571, 'TimeStamp': '2019-05-28T07:18:48.797', 'Bid': 1.437e-05, 'Ask': 1.461e-05, 'OpenBuyOrders': 42, 'OpenSellOrders': 738, 'PrevDay': 1.445e-05, 'Created': '2017-03-23T00:10:19.403'}, {'MarketName': 'USD-USDT', 'High': 1.009, 'Low': 1.00095, 'Volume': 283483.08497639, 'Last': 1.005, 'BaseVolume': 284948.3504794, 'TimeStamp': '2019-05-28T07:18:48.797', 'Bid': 1.002, 'Ask': 1.00481, 'OpenBuyOrders': 162, 'OpenSellOrders': 97, 'PrevDay': 1.00899, 'Created': '2018-05-31T13:27:08.477'}]} Bittrex_Mrkt_0 = ((Bittrex_Markets)['result'][0]) Print (Bittrex_Mrkt_0) >>> {'MarketCurrency': 'LTC', 'BaseCurrency': 'BTC', 'MarketCurrencyLong': 'Litecoin', 'BaseCurrencyLong': 'Bitcoin', 'MinTradeSize': 0.01686767, 'MarketName': 'BTC-LTC', 'IsActive': True, 'IsRestricted': False, 'Created': '2014-02-13T00:00:00', 'Notice': None, 'IsSponsored': None, 'LogoUrl': 'https://bittrexblobstorage.blob.core.windows.net/public/6defbc41-582d-47a6-bb2e-d0fa88663524.png'} Bittrex_Mrkt_Summary_0 = ((Bittrex_Get_Market_Summaries)['result'][0]) Print (Bittrex_Mrkt_Summary_0) >>> {'MarketName': 'BTC-DYN', 'High': 1.657e-05, 'Low': 1.437e-05, 'Volume': 83445.47466841, 'Last': 1.468e-05, 'BaseVolume': 1.27935571, 'TimeStamp': '2019-05-28T07:18:48.797', 'Bid': 1.437e-05, 'Ask': 1.461e-05, 'OpenBuyOrders': 42, 'OpenSellOrders': 738, 'PrevDay': 1.445e-05, 'Created': '2017-03-23T00:10:19.403'} if ((Bittrex_Mrkt_0)['IsActive']) == True: if ((Bittrex_Mrkt_0['IsRestricted'])) == False: THEN HERE: I need to return a dictionary from the list of dictionaries in Bittrex_Get_Market_Summaries, and I need to find that dictionary not by its index value but by it containing a 'MarketName': key value. ('\''+(Bittrex_Mrkt_0)['BaseCurrency']+'-'+(Bittrex_Mrkt_0)['MarketCurrency']+'\'') 'BTC-LTC' That could be inserted after 'MarketName': but I don't know what to do beyond that. I'm still very new to Python. Maybe a for loop is what I'm looking for here? In other words: I need information from BOTH of those links. The first link gives the Active True/False and IsRestricted True/False information to the if statements. Based on that, the Bid and Ask values are going to be pulled next for the same BTC-LTC pair. So I can't go by the index in the list since they are totally different and will always possibly change. I have to use 'BTC-LTC' as a way to search the second list of dictionaires, NOT the index. RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - heiner55 - May-28-2019 Do you want this ? #!/usr/bin/python3 import sys, pprint, requests url = "https://api.bittrex.com/api/v1.1/public/getmarkets" rsp = requests.get(url) if rsp.status_code == 200: markets = rsp.json()["result"] #pprint.pprint(markets) else: print("ERROR:", rsp.status_code) print(rsp.text) sys.exit(1) url = "https://api.bittrex.com/api/v1.1/public/getmarketsummaries" rsp = requests.get(url) if rsp.status_code == 200: markets_sum = rsp.json()["result"] #pprint.pprint(markets_sum) else: print("ERROR:", rsp.status_code) print(rsp.text) sys.exit(1) for market in markets: if market['IsActive'] == True and market['IsRestricted'] == False: ticker = market['BaseCurrency'] + '-' + market['MarketCurrency'] print(ticker, end="") for market_sum in markets_sum: if ticker == market_sum['MarketName']: print(" found", end="") break print(".") #done
RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - dn237 - May-28-2019 I'm really grateful for someone trying to understand that mess of a post I made earlier but I think I was more tired than I realized and just posted a confusing mess lol. That's not what I am looking for. I need to search Bittrex_Get_Market_Summaries for 'MarketName': 'BTC-LTC' rather than index 0. How do I do that? Also what gets returned should be the entire dictionary containing 'MarketName': 'BTC-LTC'. So I need to search through all of Bittrex_Get_Market_Summaries for a dictionary containing the key value 'MarketName': 'BTC-LTC' and have that dictionary print or whatever. RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - heiner55 - May-28-2019 I thought I have done this: for market in markets: if market['IsActive'] == True and market['IsRestricted'] == False: ticker = market['BaseCurrency'] + '-' + market['MarketCurrency'] print(ticker, end="") for market_sum in markets_sum: if ticker == market_sum['MarketName']: print(" found", end="") break print(".") RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - dn237 - May-28-2019 (May-28-2019, 12:49 PM)heiner55 Wrote: I thought I have done this: Maybe I just don't understand that code but the output seems different than what I'm needing. The IsActive and IsRestricted comes from Bittrex_Markets. So if say BTC-LTC is True and False for those, then I need to search Bittrex_Get_Market_Summaries for a dictionary containing the key value 'MarketName': 'BTC-LTC' to print that entire dictionary. This next bit is unrelated, but once I have that entire dictionary I will be using the bid and ask from it. I don't need code just saying if any of them are found or not. If say 'BTC-LTC' is both True and False, I already know it is somewhere in the second variable Bittrex_Get_Market_Summaries. I think it may be what I need. I think found can be changed to return the dictionary. I'm very new to Python so I apologize for being inept. RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - heiner55 - May-28-2019 #!/usr/bin/python3 import sys, pprint, requests url = "https://api.bittrex.com/api/v1.1/public/getmarkets" rsp = requests.get(url) if rsp.status_code == 200: markets = rsp.json()["result"] #pprint.pprint(markets) else: print("ERROR:", rsp.status_code) print(rsp.text) sys.exit(1) url = "https://api.bittrex.com/api/v1.1/public/getmarketsummaries" rsp = requests.get(url) if rsp.status_code == 200: markets_sum = rsp.json()["result"] #pprint.pprint(markets_sum) else: print("ERROR:", rsp.status_code) print(rsp.text) sys.exit(1) for market in markets: if market['IsActive'] == True and market['IsRestricted'] == False: ticker = market['BaseCurrency'] + '-' + market['MarketCurrency'] #print(ticker) for market_sum in markets_sum: if ticker == market_sum['MarketName']: print(market_sum) break print() #done
RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - dn237 - May-28-2019 That's what I've been trying to figure out how to do! Thanks! You're awesome! I will study for loops more now to understand this better. RE: Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV - heiner55 - May-28-2019 Super. Here the latest code: #!/usr/bin/python3 import sys, pprint, requests # get data1 url = "https://api.bittrex.com/api/v1.1/public/getmarkets" rsp = requests.get(url) if rsp.status_code == 200: markets = rsp.json()["result"] #pprint.pprint(markets) else: print("ERROR:", rsp.status_code) print(rsp.text) sys.exit(1) # get data2 url = "https://api.bittrex.com/api/v1.1/public/getmarketsummaries" rsp = requests.get(url) if rsp.status_code == 200: marketsummaries = rsp.json()["result"] #pprint.pprint(marketsummaries) else: print("ERROR:", rsp.status_code) print(rsp.text) sys.exit(1) # find all tickers which are active and not restricted list_found = [ ] for market in markets: if market['IsActive'] == True and market['IsRestricted'] == False: ticker = market['BaseCurrency'] + '-' + market['MarketCurrency'] for marketsummary in marketsummaries: if ticker == marketsummary['MarketName']: list_found += [ marketsummary ] break # do something with list pprint.pprint(list_found) #done
|