简体   繁体   中英

Append list nested dictionary based on dictionary value

Edited here... I am getting real time data like below from following current_tick = json.loads(response)

{'symbol': 'TSLA', 'minute': '2020/06/02 17:31', 'open': '882.4', 'high': '882.4', 'low': '882.93', 'close': '885.93'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:33', 'open': '307.96', 'high': '307.99', 'low': '307.96', 'close': '307.99'},
{'symbol': 'TSLA', 'minute': '2020/06/02 17:32', 'open': '882.0', 'high': '882.0', 'low': '307.94', 'close': '307.94'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:34', 'open': '307.94', 'high': '307.97', 'low': '307.94', 'close': '307.96'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:35', 'open': '307.96', 'high': '307.98', 'low': '307.96', 'close': '307.98'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:36', 'open': '308.02', 'high': '308.04', 'low': '308.02', 'close': '308.04'}

I have inserted like below in variable current_tick:

Edited here...

{'symbol': 'TSLA', 'minute': '2020/06/02 17:31', 'open': '882.4', 'high': '882.4', 'low': '882.93', 'close': '885.93'}
{'symbol': 'SPY', 'minute': '2020/06/02 17:33', 'open': '307.96', 'high': '307.99', 'low': '307.96', 'close': '307.99'}
{'symbol': 'TSLA', 'minute': '2020/06/02 17:32', 'open': '882.0', 'high': '882.0', 'low': '307.94', 'close': '307.94'}
{'symbol': 'SPY', 'minute': '2020/06/02 17:34', 'open': '307.94', 'high': '307.97', 'low': '307.94', 'close': '307.96'}
{'symbol': 'SPY', 'minute': '2020/06/02 17:35', 'open': '307.96', 'high': '307.98', 'low': '307.96', 'close': '307.98'}
{'symbol': 'SPY', 'minute': '2020/06/02 17:36', 'open': '308.02', 'high': '308.04', 'low': '308.02', 'close': '308.04'}

I have implemented code which works fine if the stock symbol is one and takes data from variable current_tick, however if it is more than one stock symbol then it overwrites existing one and lead to other issue.

if not tick_dt in minutes_processed:
    minutes_processed[tick_dt] = True
    print(minutes_processed)

    if len(minute_candlesticks) > 0:
        minute_candlesticks[-1]["close"] = previous_tick["last"]

    minute_candlesticks.append(
        {
            "symbol": current_tick["symbol"],
            "minute": tick_dt,
            "open": current_tick["last"],
            "high": current_tick["last"],
            "low": current_tick["last"],
        }
    )

if len(minute_candlesticks) > 0:
    current_candlestick = minute_candlesticks[-1]
    if current_tick["last"] > current_candlestick["high"]:
        current_candlestick["high"] = current_tick["last"]
    if current_tick["last"] < current_candlestick["low"]:
        current_candlestick["low"] = current_tick["last"]

Now I am trying to create list of dictionaries on dictionary and insert stock symbols live data to map their specific symbol like below.

app_stock_list = [
{'TSLA': {'symbol': 'TSLA', 'minute': '2020/06/02 17:31', 'open': '882.4', 'high': '882.4', 'low': '882.93', 'close': '885.93'},
{'symbol': 'TSLA', 'minute': '2020/06/02 17:32', 'open': '882.0', 'high': '882.0', 'low': '307.94', 'close': '307.94'}},

{'SPY': {'symbol': 'SPY', 'minute': '2020/06/02 17:33', 'open': '307.96', 'high': '307.99', 'low': '307.96', 'close': '307.99'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:34', 'open': '307.94', 'high': '307.97', 'low': '307.94', 'close': '307.96'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:35', 'open': '307.96', 'high': '307.98', 'low': '307.96', 'close': '307.98'},
{'symbol': 'SPY', 'minute': '2020/06/02 17:36', 'open': '308.02', 'high': '308.04', 'low': '308.02', 'close': '308.04'}
]

How do I correct the above code to create as above dictionary or what is the correct way to write a code to append live data for each symbol?

Note: I edited little bit to have a correct understanding.

I think this dict comprehension will work for you:

stock_list = [
    {'symbol': 'TSLA', 'minute': '2020/06/02 17:31', 'open': '882.4', 'high': '882.4', 'low': '882.93', 'close': '885.93'},
    {'symbol': 'SPY', 'minute': '2020/06/02 17:33', 'open': '307.96', 'high': '307.99', 'low': '307.96', 'close': '307.99'},
    {'symbol': 'TSLA', 'minute': '2020/06/02 17:32', 'open': '882.0', 'high': '882.0', 'low': '307.94', 'close': '307.94'},
    {'symbol': 'SPY', 'minute': '2020/06/02 17:34', 'open': '307.94', 'high': '307.97', 'low': '307.94', 'close': '307.96'},
    {'symbol': 'SPY', 'minute': '2020/06/02 17:35', 'open': '307.96', 'high': '307.98', 'low': '307.96', 'close': '307.98'},
    {'symbol': 'SPY', 'minute': '2020/06/02 17:36', 'open': '308.02', 'high': '308.04', 'low': '308.02', 'close': '308.04'}
]

stocks = {symbol: [i for i in stock_list if i['symbol'] == symbol] 
          for symbol in {i['symbol'] for i in stock_list}} # use a set() comprehension to get the unique items

# how to update with a price dict
updated_price = {'symbol': 'TSLA', 'minute': '2020/06/02 17:33', 'open': '123.0', 'high': '456.0', 'low': '789.94', 'close': '032.94'}
stocks[updated_price['symbol']].append(updated_price)

import json
print(json.dumps(stocks, indent=4))

Output (jsonified):

{
    "TSLA": [
        {
            "symbol": "TSLA",
            "minute": "2020/06/02 17:31",
            "open": "882.4",
            "high": "882.4",
            "low": "882.93",
            "close": "885.93"
        },
        {
            "symbol": "TSLA",
            "minute": "2020/06/02 17:32",
            "open": "882.0",
            "high": "882.0",
            "low": "307.94",
            "close": "307.94"
        },
        {
            "symbol": "TSLA",
            "minute": "2020/06/02 17:33",
            "open": "123.0",
            "high": "456.0",
            "low": "789.94",
            "close": "032.94"
        }
    ],
    "SPY": [
        {
            "symbol": "SPY",
            "minute": "2020/06/02 17:33",
            "open": "307.96",
            "high": "307.99",
            "low": "307.96",
            "close": "307.99"
        },
        {
            "symbol": "SPY",
            "minute": "2020/06/02 17:34",
            "open": "307.94",
            "high": "307.97",
            "low": "307.94",
            "close": "307.96"
        },
        {
            "symbol": "SPY",
            "minute": "2020/06/02 17:35",
            "open": "307.96",
            "high": "307.98",
            "low": "307.96",
            "close": "307.98"
        },
        {
            "symbol": "SPY",
            "minute": "2020/06/02 17:36",
            "open": "308.02",
            "high": "308.04",
            "low": "308.02",
            "close": "308.04"
        }
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM