Related
i have two(n) websocket messages input like this, separated by "==new msg=="
(don't know why but some of the messages are longer..)
++Rcv raw: b'\x81~\x02\xff{"data":[{"c":null,"p":20926.99,"s":"BINANCE:BTCUSDT","t":1656017217026,"v":0.0008},{"c":null,"p":20927.45,"s":"BINANCE:BTCUSDT","t":1656017217056,"v":0.0005},{"c":null,"p":20930.51,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.001},{"c":null,"p":20930.51,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.00061},{"c":null,"p":20930.52,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.1},{"c":null,"p":20931.18,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.05098},{"c":null,"p":20930.5,"s":"BINANCE:BTCUSDT","t":1656017217201,"v":0.03},{"c":null,"p":20930.5,"s":"BINANCE:BTCUSDT","t":1656017217201,"v":0.03654},{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.03},{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.01882}],"type":"trade"}'
++Rcv decoded: fin=1 opcode=1 data=b'{"data":[{"c":null,"p":20926.99,"s":"BINANCE:BTCUSDT","t":1656017217026,"v":0.0008},{"c":null,"p":20927.45,"s":"BINANCE:BTCUSDT","t":1656017217056,"v":0.0005},{"c":null,"p":20930.51,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.001},{"c":null,"p":20930.51,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.00061},{"c":null,"p":20930.52,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.1},{"c":null,"p":20931.18,"s":"BINANCE:BTCUSDT","t":1656017217079,"v":0.05098},{"c":null,"p":20930.5,"s":"BINANCE:BTCUSDT","t":1656017217201,"v":0.03},{"c":null,"p":20930.5,"s":"BINANCE:BTCUSDT","t":1656017217201,"v":0.03654},{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.03},{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.01882}],"type":"trade"}'
==new msg==
++Rcv raw: b'\x81~\x03\x03{"data":[{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.08477},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.1},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.25},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.07053},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217694,"v":0.08207},{"c":null,"p":20926.98,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.05098},{"c":null,"p":20926.31,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.1},{"c":null,"p":20925.5,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.16424},{"c":null,"p":20927.87,"s":"BINANCE:BTCUSDT","t":1656017217711,"v":0.00052},{"c":null,"p":20923.16,"s":"BINANCE:BTCUSDT","t":1656017217927,"v":0.0155}],"type":"trade"}'
++Rcv decoded: fin=1 opcode=1 data=b'{"data":[{"c":null,"p":20927.9,"s":"BINANCE:BTCUSDT","t":1656017217649,"v":0.08477},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.1},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.25},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217678,"v":0.07053},{"c":null,"p":20927.89,"s":"BINANCE:BTCUSDT","t":1656017217694,"v":0.08207},{"c":null,"p":20926.98,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.05098},{"c":null,"p":20926.31,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.1},{"c":null,"p":20925.5,"s":"BINANCE:BTCUSDT","t":1656017217698,"v":0.16424},{"c":null,"p":20927.87,"s":"BINANCE:BTCUSDT","t":1656017217711,"v":0.00052},{"c":null,"p":20923.16,"s":"BINANCE:BTCUSDT","t":1656017217927,"v":0.0155}],"type":"trade"}'
==new msg==
and i have some code so far
import websocket
import json
from collections import ChainMap
y = []
c = []
def on_message(ws, message):
print("==new msg==")
y.append(json.loads(message))
def on_error(ws, error):
print(error)
def on_close(ws,close_status_code, close_reason):
print("==closed==")
print(len(y))
c = dict(ChainMap(*y))
print(c)
def on_open(ws):
ws.send('{"type":"subscribe","symbol":"BINANCE:BTCUSDT"}')
if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("wss://ws.finnhub.io?token=****ps2ad3iahju47mdg",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
the output is:
==closed==
2
{'data': [{'c': None, 'p': 20926.99, 's': 'BINANCE:BTCUSDT', 't': 1656017217026, 'v': 0.0008}, {'c': None, 'p': 20927.45, 's': 'BINANCE:BTCUSDT', 't': 1656017217056, 'v': 0.0005}, {'c': None, 'p': 20930.51, 's': 'BINANCE:BTCUSDT', 't': 1656017217079, 'v': 0.001}, {'c': None, 'p': 20930.51, 's': 'BINANCE:BTCUSDT', 't': 1656017217079, 'v': 0.00061}, {'c': None, 'p': 20930.52, 's': 'BINANCE:BTCUSDT', 't': 1656017217079, 'v': 0.1}, {'c': None, 'p': 20931.18, 's': 'BINANCE:BTCUSDT', 't': 1656017217079, 'v': 0.05098}, {'c': None, 'p': 20930.5, 's': 'BINANCE:BTCUSDT', 't': 1656017217201, 'v': 0.03}, {'c': None, 'p': 20930.5, 's': 'BINANCE:BTCUSDT', 't': 1656017217201, 'v': 0.03654}, {'c': None, 'p': 20927.9, 's': 'BINANCE:BTCUSDT', 't': 1656017217649, 'v': 0.03}, {'c': None, 'p': 20927.9, 's': 'BINANCE:BTCUSDT', 't': 1656017217649, 'v': 0.01882}], 'type': 'trade'}
only from the first message..?
The goal is to calculate the volume-weighted average price of trades made during a 1 min interval
output should look like this:
2022-05-04 12:22:33 price:20926.99 volume:0.0008
2022-05-04 12:23:43 price:20926.99 volume:0.0058
2022-05-04 12:24:53 price:20927.99 volume:0.0608
Hi apologies for the noob question...
I have written some code:
with RESTClient(key) as client:
from_ = "2022-01-01"
to = "2022-02-01"
for i in all_tickers:
ticker = i['ticker']
resp = client.stocks_equities_aggregates(ticker, 1, "day", from_, to, unadjusted=False)
print(f"Minute aggregates for {resp.ticker} between {from_} and {to}.")
try:
for result in resp.results:
print(result)
except:
print('nothing')
Which outputs:
Minute aggregates for A between 2022-01-01 and 2022-02-01.
{'v': 1606323.0, 'vw': 155.8021, 'o': 159, 'c': 156.48, 'h': 159.44, 'l': 153.93, 't': 1641186000000, 'n': 24318}
{'v': 2233958.0, 'vw': 151.518, 'o': 155.49, 'c': 151.19, 'h': 155.63, 'l': 149.7, 't': 1641272400000, 'n': 34707}
{'v': 2370529.0, 'vw': 149.9716, 'o': 150.83, 'c': 148.6, 'h': 153.1, 'l': 148.53, 't': 1641358800000, 'n': 27421}
{'v': 2298277.0, 'vw': 148.4397, 'o': 148.85, 'c': 149.12, 'h': 149.96, 'l': 145.58, 't': 1641445200000, 'n': 34441}
{'v': 2058658.0, 'vw': 146.4352, 'o': 149.12, 'c': 145.15, 'h': 149.73, 'l': 145.09, 't': 1641531600000, 'n': 28611}
{'v': 2548145.0, 'vw': 143.2162, 'o': 143.29, 'c': 145.16, 'h': 145.31, 'l': 140.86, 't': 1641790800000, 'n': 37241}
{'v': 2194208.0, 'vw': 146.0091, 'o': 145, 'c': 146.64, 'h': 146.94, 'l': 143.81, 't': 1641877200000, 'n': 22781}
{'v': 2250847.0, 'vw': 149.3025, 'o': 147.8, 'c': 149.51, 'h': 150.39, 'l': 147.55, 't': 1641963600000, 'n': 27392}
{'v': 1741764.0, 'vw': 145.7333, 'o': 149.46, 'c': 145.17, 'h': 149.54, 'l': 144.85, 't': 1642050000000, 'n': 23437}
{'v': 2225442.0, 'vw': 143.9446, 'o': 144.04, 'c': 144.68, 'h': 145.15, 'l': 142.36, 't': 1642136400000, 'n': 28295}
{'v': 1907368.0, 'vw': 141.2762, 'o': 142.42, 'c': 140.47, 'h': 143.24, 'l': 140.34, 't': 1642482000000, 'n': 27031}
{'v': 1472206.0, 'vw': 141.538, 'o': 140.67, 'c': 140.43, 'h': 143.6, 'l': 140.26, 't': 1642568400000, 'n': 23595}
{'v': 1861384.0, 'vw': 140.9367, 'o': 141.38, 'c': 139.48, 'h': 143.14, 'l': 139.05, 't': 1642654800000, 'n': 26612}
{'v': 1878663.0, 'vw': 138.4591, 'o': 139.54, 'c': 137.51, 'h': 140.49, 'l': 137.49, 't': 1642741200000, 'n': 27133}
{'v': 2155299.0, 'vw': 135.3192, 'o': 136.38, 'c': 138.12, 'h': 138.49, 'l': 131.28, 't': 1643000400000, 'n': 32745}
{'v': 1705313.0, 'vw': 134.473, 'o': 135.36, 'c': 134.57, 'h': 136.62, 'l': 132.65, 't': 1643086800000, 'n': 24457}
{'v': 1999575.0, 'vw': 134.5836, 'o': 135.54, 'c': 133.51, 'h': 138.0454, 'l': 132.27, 't': 1643173200000, 'n': 28088}
{'v': 1715819.0, 'vw': 133.1775, 'o': 135.28, 'c': 132.09, 'h': 136.36, 'l': 131.68, 't': 1643259600000, 'n': 25556}
{'v': 2174805.0, 'vw': 135.3363, 'o': 133, 'c': 137.06, 'h': 137.4, 'l': 131.215, 't': 1643346000000, 'n': 21446}
{'v': 1702950.0, 'vw': 138.8672, 'o': 137.32, 'c': 139.32, 'h': 139.47, 'l': 136.9729, 't': 1643605200000, 'n': 21984}
{'v': 1655987.0, 'vw': 140.2601, 'o': 140.53, 'c': 141.03, 'h': 141.27, 'l': 138.45, 't': 1643691600000, 'n': 25755}
Minute aggregates for AA between 2022-01-01 and 2022-02-01.
{'v': 6208442.0, 'vw': 60.9882, 'o': 60.24, 'c': 60.36, 'h': 62.61, 'l': 60.09, 't': 1641186000000, 'n': 49209}
{'v': 7943653.0, 'vw': 58.135, 'o': 60.68, 'c': 57.53, 'h': 61.15, 'l': 57.21, 't': 1641272400000, 'n': 59085}
{'v': 7599751.0, 'vw': 60.0291, 'o': 58.95, 'c': 58.55, 'h': 61.79, 'l': 58.445, 't': 1641358800000, 'n': 65793}
{'v': 4363058.0, 'vw': 58.3964, 'o': 58.94, 'c': 58.45, 'h': 59.4911, 'l': 57.25, 't': 1641445200000, 'n': 39017}
{'v': 8071270.0, 'vw': 61.7246, 'o': 60.14, 'c': 62.37, 'h': 62.89, 'l': 59.65, 't': 1641531600000, 'n': 62728}
{'v': 5653472.0, 'vw': 61.268, 'o': 61.62, 'c': 61.54, 'h': 62.71, 'l': 60.441, 't': 1641790800000, 'n': 43504}
{'v': 6003582.0, 'vw': 61.0876, 'o': 60.71, 'c': 62.2, 'h': 62.25, 'l': 59.12, 't': 1641877200000, 'n': 50028}
{'v': 6434989.0, 'vw': 62.1745, 'o': 63.66, 'c': 61.88, 'h': 64.37, 'l': 60.86, 't': 1641963600000, 'n': 53063}
{'v': 5769838.0, 'vw': 61.5961, 'o': 61.75, 'c': 60.51, 'h': 63.26, 'l': 60.37, 't': 1642050000000, 'n': 46975}
{'v': 4397108.0, 'vw': 60.5607, 'o': 60.27, 'c': 61.39, 'h': 61.44, 'l': 59.34, 't': 1642136400000, 'n': 41558}
{'v': 5994091.0, 'vw': 60.0163, 'o': 60.5, 'c': 60.05, 'h': 61.56, 'l': 58.8, 't': 1642482000000, 'n': 55228}
{'v': 7851084.0, 'vw': 60.0468, 'o': 61.39, 'c': 59.63, 'h': 61.93, 'l': 58.885, 't': 1642568400000, 'n': 62775}
{'v': 15925959.0, 'vw': 62.0662, 'o': 62.1, 'c': 61.25, 'h': 64.25, 'l': 59.97, 't': 1642654800000, 'n': 127187}
{'v': 11024982.0, 'vw': 57.5373, 'o': 60.02, 'c': 56.21, 'h': 60.15, 'l': 56.04, 't': 1642741200000, 'n': 99811}
{'v': 9209629.0, 'vw': 56.091, 'o': 53.81, 'c': 58.02, 'h': 58.2, 'l': 53.26, 't': 1643000400000, 'n': 83494}
{'v': 7780587.0, 'vw': 59.9004, 'o': 57.51, 'c': 61.21, 'h': 61.6, 'l': 56.7608, 't': 1643086800000, 'n': 66222}
{'v': 9267426.0, 'vw': 61.8703, 'o': 61.54, 'c': 60.75, 'h': 63.64, 'l': 59.88, 't': 1643173200000, 'n': 76224}
{'v': 6445290.0, 'vw': 59.0007, 'o': 60.6, 'c': 58.03, 'h': 61.6599, 'l': 57.47, 't': 1643259600000, 'n': 55506}
{'v': 6987869.0, 'vw': 56.9155, 'o': 58, 'c': 57.4, 'h': 58.39, 'l': 55.58, 't': 1643346000000, 'n': 62130}
{'v': 7206053.0, 'vw': 56.1436, 'o': 56.94, 'c': 56.71, 'h': 57.02, 'l': 55.02, 't': 1643605200000, 'n': 59567}
{'v': 5939348.0, 'vw': 57.6426, 'o': 57.99, 'c': 58.17, 'h': 58.44, 'l': 56.73, 't': 1643691600000, 'n': 55371}
...
When I try to put that data in an array, like so:
with RESTClient(key) as client:
from_ = "2022-01-01"
to = "2022-02-01"
arr = []
for i in all_tickers:
ticker = i['ticker']
resp = client.stocks_equities_aggregates(ticker, 1, "day", from_, to, unadjusted=False)
print(f"Minute aggregates for {resp.ticker} between {from_} and {to}.")
try:
for result in resp.results:
arr.extend(result)
print(arr)
except:
print('nothing')
It is just saving the keys:
Minute aggregates for A between 2022-01-01 and 2022-02-01.
['v', 'vw', 'o', 'c', 'h', 'l', 't', 'n']
['v', 'vw', 'o', 'c', 'h', 'l', 't', 'n', 'v', 'vw', 'o', 'c', 'h', 'l', 't', 'n']
['v', 'vw', 'o', 'c', 'h', 'l', 't', 'n', 'v', 'vw', 'o', 'c', 'h', 'l', 't', 'n', 'v', 'vw', 'o', 'c', 'h', 'l', 't', 'n']
['v', 'vw', 'o', 'c', 'h', 'l', 't', 'n', 'v', 'vw', 'o', 'c', 'h', 'l', 't', 'n', 'v', 'vw', 'o', 'c', 'h', 'l', 't', 'n', 'v', 'vw', 'o', 'c', 'h', 'l', 't', 'n']
How do I make it so the entire object is put into the array?
When I say the entire object I mean all the keys and values, e.g:
arr = [{'v': 6208442.0, 'vw': 60.9882, 'o': 60.24, 'c': 60.36, 'h': 62.61, 'l': 60.09, 't': 1641186000000, 'n': 49209}, {'v': 6208442.0, 'vw': 60.9882, 'o': 60.24, 'c': 60.36, 'h': 62.61, 'l': 60.09, 't': 1641186000000, 'n': 49209}, ...]
Any idea what I'm doing wrong?
Instead of this:
for result in resp.results:
arr.extend(result)
... try this:
for result in resp.results:
arr.append(result)
... or this:
arr.extend(resp.results)
One of those (or possibly both) should work.
I am accessing an API through a get request from which I get a response.
This is my code:
import requests
import json
symbol = "AAPL"
end_point="https://api.polygon.io/v2/snapshot/locale/us/markets/stocks/tickers/"+symbol+"?apiKey=my_key"
r=requests.get(end_point).json()
print(r)
print(r['min'])
print(r) returned:
{'request_id': '2735c0be51de7719fd99460fe8696080', 'status': 'OK', 'ticker': {'day': {'c': 172.93, 'h': 175.48, 'l': 172.37, 'o': 174.14, 'v': 65575561, 'vw': 174.0984}, 'lastQuote': {'P': 172.83, 'S': 3, 'p': 172.82, 's': 1, 't': 1644524332922450142}, 'lastTrade': {'c': None, 'i': '139592', 'p': 172.8199, 's': 2014, 't': 1644524331573573011, 'x': 4}, 'min': {'av': 65559987, 'c': 172.9, 'h': 173.14, 'l': 172.89, 'o': 173.09, 'v': 107429, 'vw': 173.0138}, 'prevDay': {'c': 176.28, 'h': 176.65, 'l': 174.9, 'o': 176.05, 'v': 71204538, 'vw': 175.8287}, 'ticker': 'AAPL', 'todaysChange': -3.46, 'todaysChangePerc': -1.963, 'updated': 1644524331573573011}}
but when I try to access the key "min", I get key error:
KeyError: 'min'
This is super-simple. What am I doing wrong?
Please try:
r['ticker']['min']
the key min is inside ticker. Do this:
r['ticker']['min']
Here we go again. One 'fairly easy' exercise that has finally given me headache.
Make a program that uses a lookup table to convert any set of alphabets into their corresponding NATO phonetic alphabets (https://en.wikipedia.org/wiki/NATO_phonetic_alphabet). Also implement the inverse function.
Input: cat
Output: charlie alfa tango
Inverse function:
Input : charlie alfa tango
Output : cat
I have defined a dictionary with the alphabet and its Nato equivalent for each letter. Then, I have defined a function that will take elements from a list as input. Letter corresponds to my keys in the dictionary and letter conversion corresponds to my values. The function works in the following cases:
When I enter a single character, it will give me its 'Nato' equivalent. (comparing the string with letter and outputting the letter conversion)
When I enter a string that is Nato alphabet it will output the letter. (comparing the string with the letter conversion and outputting the letter)
I'm getting stuck when I need to evaluate the lenght of the string from my list, and then
convert each character into its Nato alphabet.
nato_alphabet = {'a':'Alfa', 'A': 'Alfa', 'b': 'Bravo', 'B': 'Bravo', 'c': 'Charlie', 'C': 'Charlie', 'd': 'Delta', 'D':'Delta', 'e': 'Echo', 'E':'Echo', 'f': 'Foxtrot', 'F': 'Foxtrot', 'G': 'Golf', 'g': 'Golf', 'h': 'Hotel', 'H': 'Hotel', 'k': 'Kilo', 'K': 'Kilo', 'l': 'Lima', 'L': 'Lima', 'm': 'Mike', 'M': 'Mike', 'n': 'November', 'N': 'November', 'o': 'Oscar', 'O': 'Oscar',
'p': 'Papa', 'P': 'Papa', 'q': 'Quebec', 'Q': 'Quebec', 'r': 'Romeo', 'R': 'Romeo', 's': 'Sierra', 'S': 'Sierra', 't':'Tango', 'T': 'Tango', 'u': 'Uniform', 'U': 'Uniform', 'V': 'Victor', 'v': 'Victor', 'w': 'Whiskey', 'W': 'Whiskey', 'y': 'Yankee', 'Y': 'Yankee', 'Z': 'Zulu', 'z': 'Zulu'}
#Passing through a list
def convert(word_to_convert):
word_converted= []
for i in word_to_convert:
for letter, letter_conversion in nato_alphabet.items():
if i == letter:
word_converted.append(letter_conversion)
if i == letter_conversion:
word_converted.append(letter)
if len(i) == letter:
word_converted.append(letter_conversion )
return word_converted
print(convert(['c', 'Alfa', 'moon']))
Thank you everyone for your help !
Diana
Use:
def convert(word_to_convert):
word_converted = []
for i in word_to_convert:
word_converted.append(" ".join(nato_alphabet[ii.lower()] for ii in i))
return word_converted
print(convert(['c', 'Alfa', 'moon']))
Output
['Charlie', 'Alfa Lima Foxtrot Alfa', 'Mike Oscar Oscar November']
As an alternative:
# Passing through a list
def convert(words_to_convert):
word_converted = []
# iterate over each word
for word in words_to_convert:
nato_encoded_list = []
# iterate over each character in word
for char in word:
# fetch the corresponding value from nato_alphabet
nato_encoded_list.append(nato_alphabet[char.lower()])
# join the encoded words and add to word_converted list
word_converted.append(" ".join(nato_encoded_list))
return word_converted
Basically iterate over each, word and for each character in the word fetch the nato_alphabet value.
This might let me know if something is unclear
nato_alphabet = {'a':'Alfa', 'A': 'Alfa', 'b': 'Bravo', 'B': 'Bravo', 'c': 'Charlie', 'C': 'Charlie', 'd': 'Delta', 'D':'Delta', 'e': 'Echo', 'E':'Echo', 'f': 'Foxtrot', 'F': 'Foxtrot', 'G': 'Golf', 'g': 'Golf', 'h': 'Hotel', 'H': 'Hotel', 'k': 'Kilo', 'K': 'Kilo', 'l': 'Lima', 'L': 'Lima', 'm': 'Mike', 'M': 'Mike', 'n': 'November', 'N': 'November', 'o': 'Oscar', 'O': 'Oscar',
'p': 'Papa', 'P': 'Papa', 'q': 'Quebec', 'Q': 'Quebec', 'r': 'Romeo', 'R': 'Romeo', 's': 'Sierra', 'S': 'Sierra', 't':'Tango', 'T': 'Tango', 'u': 'Uniform', 'U': 'Uniform', 'V': 'Victor', 'v': 'Victor', 'w': 'Whiskey', 'W': 'Whiskey', 'y': 'Yankee', 'Y': 'Yankee', 'Z': 'Zulu', 'z': 'Zulu'}
nato_alphabet_inverse = {v.lower():k.lower() for k,v in nato_alphabet.items()}
nato_alphabet = {k.lower():v.lower() for k,v in nato_alphabet.items()}
# print(nato_alphabet_inverse)
def convert(list_to_convert):
return_list = []
for each_item in list_to_convert:
# print(each_item)
if " " in each_item: # Sentence thus must be converted to Alphabets
l_ = each_item.split()
temp_list = []
for each_word in l_:
# print(each_word)
temp_list.append(nato_alphabet_inverse[each_word.lower()])
# return_list.append()
return_list.append("".join(temp_list))
else: # Convert to equivalent Nato
temp_list = []
for chr in each_item:
# print(chr)
temp_list.append(nato_alphabet[chr.lower()])
str_ = " ".join(temp_list)
return_list.append(str_)
return return_list
print(convert(['cAT',"charlie alfa tango"]))
I am trying to extract information from a list within a list within a list to end up with something like this from the information below: ('h': '0.77584', 'l': '0.77292'), ('h': '0.77521', 'l': '0.77206')
print(dict)
[{'complete': True, 'volume': 2290, 'time': '2021-01-15', 'mid': {'o': '0.77540', 'h': '0.77584', 'l': '0.77292', 'c': '0.77440'}}, {'complete': True, 'volume': 2312, 'time': '2021-01-15', 'mid': {'o': '0.77436', 'h': '0.77521', 'l': '0.77206', 'c': '0.77206'}}]
Not sure how to go about it. I tried
something = list(list(dict.items())[0].items())[3][1]
print(something)
However, this returned {'o': '0.77540', 'h': '0.77584', 'l': '0.77292', 'c': '0.77440'}
How to get the requested data?
You can use the following list and dict comprehension
dict = [{'complete': True, 'volume': 2290, 'time': '2021-01-15', 'mid': {'o': '0.77540', 'h': '0.77584', 'l': '0.77292', 'c': '0.77440'}}, {'complete': True, 'volume': 2312, 'time': '2021-01-15', 'mid': {'o': '0.77436', 'h': '0.77521', 'l': '0.77206', 'c': '0.77206'}}]
res = [{k:v for k, v in i['mid'].items() if k in 'hl'} for i in dict]
print(res)
Output
[{'h': '0.77584', 'l': '0.77292'}, {'h': '0.77521', 'l': '0.77206'}]
First you can take the mid child from each parent dictionary in the list.
dict = [{'complete': True, 'volume': 2290, 'time': '2021-01-15', 'mid': {'o': '0.77540', 'h': '0.77584', 'l': '0.77292', 'c': '0.77440'}}, {'complete': True, 'volume': 2312, 'time': '2021-01-15', 'mid': {'o': '0.77436', 'h': '0.77521', 'l': '0.77206', 'c': '0.77206'}}]
old_dict = [dict[0]['mid'],dict[1]['mid']]
Then loop through those entries and filter based off the keys (i.e. remove o and c):
list_you_want = []
for i in [0,1]:
list_you_want.append({ your_key: old_dict[i][your_key] for your_key in ['h','l'] })
Which gives you:
[{'h': '0.77584', 'l': '0.77292'}, {'h': '0.77521', 'l': '0.77206'}]