Python 3.x 从dict中的值创建新列表

Python 3.x 从dict中的值创建新列表,python-3.x,list,dictionary,Python 3.x,List,Dictionary,我有一本字典 {'Items': [ {'stage': 'temp', 'file_status': 'NOT PROCESSED', 'table_name': 'abcd', 'filehour_key': '20200817-22'}, {'stage': 'temp', 'file_status': 'NOT PROCESSED', 'table_name': 'abcd', 'filehour_key': '20200817-2

我有一本字典

{'Items': [
              {'stage': 'temp', 'file_status': 'NOT PROCESSED', 'table_name': 'abcd', 'filehour_key': '20200817-22'},
              {'stage': 'temp', 'file_status': 'NOT PROCESSED', 'table_name': 'abcd', 'filehour_key': '20200817-23'},
              {'stage': 'temp', 'file_status': 'NOT PROCESSED', 'table_name': 'efgh', 'filehour_key': '20200817-23'}
          ]
}
我想通过检查表名称来创建2个结果

abcd_lst = [
               {'stage': 'temp', 'file_status': 'NOT PROCESSED', 'table_name': 'abcd', 'filehour_key': '20200817-22'},
               {'stage': 'temp', 'file_status': 'NOT PROCESSED', 'table_name': 'abcd', 'filehour_key': '20200817-23'}
           ]

efgh_lst = [
               {'stage': 'temp', 'file_status': 'NOT PROCESSED', 'table_name': 'efgh', 'filehour_key': '20200817-23'}
           ]

你可以做一个简单的迭代来创建一个简单的列表目录。defaultdict也可以这样做


items = {'Items': [
              {'stage': 'temp', 'file_status': 'NOT PROCESSED', 'table_name': 'abcd', 'filehour_key': '20200817-22'},
              {'stage': 'temp', 'file_status': 'NOT PROCESSED', 'table_name': 'abcd', 'filehour_key': '20200817-23'},
              {'stage': 'temp', 'file_status': 'NOT PROCESSED', 'table_name': 'efgh', 'filehour_key': '20200817-23'}
          ]
}


import pprint
from collections import defaultdict

d = defaultdict(list)
for item in items['Items']:
    d[item['table_name']].append(item)

pprint.pprint(d)

你可以做一个简单的迭代来创建一个简单的列表目录。defaultdict也可以这样做


items = {'Items': [
              {'stage': 'temp', 'file_status': 'NOT PROCESSED', 'table_name': 'abcd', 'filehour_key': '20200817-22'},
              {'stage': 'temp', 'file_status': 'NOT PROCESSED', 'table_name': 'abcd', 'filehour_key': '20200817-23'},
              {'stage': 'temp', 'file_status': 'NOT PROCESSED', 'table_name': 'efgh', 'filehour_key': '20200817-23'}
          ]
}


import pprint
from collections import defaultdict

d = defaultdict(list)
for item in items['Items']:
    d[item['table_name']].append(item)

pprint.pprint(d)

您可以轻松地迭代数据,将表存储在列表字典中(这将有助于使用相同的
表名来“捕获”
dicts
,确保它们成对/放置在一起

下面是一个简单的代码,它可以做到这一点:

从集合导入defaultdict
tables=defaultdict(list)#创建一个默认值为list的dict
#迭代“项目”列表
对于it[‘项目’]中的dct:
table_name=dct.get('table_name')#获取名称
表[table_name].append(dct)#存储在dict中
#您可以将“tables”转换为列表
tables=list(tables.values())#包含两个表的多维列表
#你也可以打开桌子
表1、表2=表

您可以轻松地迭代数据,将表存储在列表字典中(这将有助于使用相同的
表名来“捕获”
dict
,确保它们成对/放置在一起

下面是一个简单的代码,它可以做到这一点:

从集合导入defaultdict
tables=defaultdict(list)#创建一个默认值为list的dict
#迭代“项目”列表
对于it[‘项目’]中的dct:
table_name=dct.get('table_name')#获取名称
表[table_name].append(dct)#存储在dict中
#您可以将“tables”转换为列表
tables=list(tables.values())#包含两个表的多维列表
#你也可以打开桌子
表1、表2=表

尝试列表理解:

abcd_lst = [it for it in items['Items'] if it['table_name'] == 'abcd']
efgh_lst = [it for it in items['Items'] if it['table_name'] == 'efgh']
如果您的原始字典只包含一个父键(如示例中所示),那么这是一种很好的方法

列表理解只是将for循环包装成一行,使其成为一种简洁的方式。下面是它的一般工作原理:

对于任何原始iterable,如列表:

new\u list=[for in]


您还可以添加可选的条件语句,正如我在上面所做的那样。

尝试列表理解:

abcd_lst = [it for it in items['Items'] if it['table_name'] == 'abcd']
efgh_lst = [it for it in items['Items'] if it['table_name'] == 'efgh']
如果您的原始字典只包含一个父键(如示例中所示),那么这是一种很好的方法

列表理解只是将for循环包装成一行,使其成为一种简洁的方式。下面是它的一般工作原理:

对于任何原始iterable,如列表:

new\u list=[for in]


您还可以添加一个可选的条件语句,正如我上面所做的。

Hi Ariful,您希望拆分为列表的字典是否只包含一个条目,如您的示例所示?Hi Ariful,您希望拆分为列表的字典是否只包含一个条目,如您的示例所示?