简体   繁体   English

创建包含字典列表的字典列表

[英]Create a list of dictionaries containing lists of dictionaries

I'm having some trouble trying to bend my brain around this issue I've been working on.我在试图围绕我一直在研究的这个问题上弯曲我的大脑时遇到了一些麻烦。 I have a list of stock data that I'm pulling from a SQLite database.我有一个从 SQLite 数据库中提取的股票数据列表。 This is a list of dictionaries and the dictionaries look something like this:这是一个字典列表,字典看起来像这样:

[ {symbol:'AAPL', date:'2021-04-28', close:130},
  {symbol:'AAPL', date:'2021-04-27', close:129}, 
  {symbol:'MSFT', date:'2021-04-28', close:155}, 
  {symbol:'MSFT', date:'2021-04-27', close:156}  ]  

I'm working with open, high, low, and close data and I want to write a script to assign indicator values to each data point/timestamp.我正在处理开盘价、最高价、最低价和收盘数据,我想编写一个脚本来为每个数据点/时间戳分配指标值。 In order to assign these indicators, I need to manipulate this data into a new list as follows:为了分配这些指标,我需要将这些数据操作到一个新列表中,如下所示:

Stock data = [  {'AAPL':[ {date:'2021-04-28', close:130},
                          {date:'2021-04-27', close:129} ] },
                {'MSFT':[ {date:'2021-04-28', close:155}, 
                          {date:'2021-04-27', close:156} ] } ]

I want to create a list of dictionaries with the key being the stock ticker, and the value being a list of all the OHLC data (as dictionary) contained in the database.我想创建一个字典列表,键是股票代码,值是数据库中包含的所有 OHLC 数据(作为字典)的列表。

It this seems to me to be a list of dictionaries containing lists of dictionaries.在我看来,这似乎是一个包含字典列表的字典列表。

I'm not worried about efficiency as I only want to run this once per day, in the middle of the night.我不担心效率,因为我只想在半夜每天运行一次。

This can be done with itertools.groupby with a little follow-up to remove group key.这可以通过itertools.groupby来完成,并进行一些后续操作以删除组密钥。

from itertools import groupby

lst = [{'symbol': 'AAPL', 'date': '2021-04-28', 'close': 130},
       {'symbol': 'AAPL', 'date': '2021-04-27', 'close': 129},
       {'symbol': 'MSFT', 'date': '2021-04-28', 'close': 155},
       {'symbol': 'MSFT', 'date': '2021-04-27', 'close': 156}]

group_key = 'symbol'

print([{k: [{sub_k: sub_v for sub_k, sub_v in d.items() if sub_k != group_key}
            for d in v]}
       for k, v in groupby(lst, key=lambda x: x[group_key])])

Output: Output:

[{'AAPL': [{'date': '2021-04-28', 'close': 130}, 
           {'date': '2021-04-27', 'close': 129}]}, 
 {'MSFT': [{'date': '2021-04-28', 'close': 155}, 
           {'date': '2021-04-27', 'close': 156}]}]

Edit: Assuming the list is not already ordered by 'symbol' a sorting phase is necessary:编辑:假设列表尚未按'symbol'排序,则需要排序阶段:

from itertools import groupby

lst = [{'symbol': 'AAPL', 'date': '2021-04-28', 'close': 130},
       {'symbol': 'MSFT', 'date': '2021-04-28', 'close': 155},
       {'symbol': 'AAPL', 'date': '2021-04-27', 'close': 129},
       {'symbol': 'MSFT', 'date': '2021-04-27', 'close': 156}]

group_key = 'symbol'


def get_key(x):
    return x[group_key]


print([{k: [{sub_k: sub_v for sub_k, sub_v in d.items() if sub_k != group_key}
            for d in v]}
       for k, v in groupby(sorted(lst, key=get_key), key=get_key)])

Output: Output:

[{'AAPL': [{'date': '2021-04-28', 'close': 130}, 
           {'date': '2021-04-27', 'close': 129}]}, 
 {'MSFT': [{'date': '2021-04-28', 'close': 155}, 
           {'date': '2021-04-27', 'close': 156}]}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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