Python 使用指定的映射对字典列表进行排序
Python sort a list of dicts using a specified mapping
所以,我在 python 中有一个字典列表,如下所示:
lis =
[
{'action': 'Notify', 'type': 'Something', 'Genre': 10, 'date': '2021-05-07 01:59:37'},
{'action': 'Notify', 'type': 'Something Else', 'Genre': 20, 'date': '2021-05-07 01:59:37'}
...
]
现在我希望 lis
在某种程度上,这样 每个单独的字典 都使用 mapping
我将提供的键来排序.例如,如果
mapping = {1:'date', 2:'Genre', 3:'action', 4:'type'}
然后,我想让我原来的字典列表看起来像这样:
lis =
[
{'date': '2021-05-07 01:59:37', 'Genre': 10, 'action': 'Notify', 'type': 'Something'},
{'date': '2021-05-07 01:59:37', 'Genre': 20, 'action': 'Notify', 'type': 'Something Else'}
...
]
我该如何实施?
您可以按如下方式利用 collections.OrderedDict
完成此任务
import collections
order = ['date', 'Genre', 'action', 'type']
dct1 = {'action': 'Notify', 'type': 'Something', 'Genre': 10, 'date': '2021-05-07 01:59:37'}
dct2 = {'action': 'Notify', 'type': 'Something Else', 'Genre': 20, 'date': '2021-05-07 01:59:37'}
odct1 = collections.OrderedDict.fromkeys(order)
odct1.update(dct1)
odct2 = collections.OrderedDict.fromkeys(order)
odct2.update(dct2)
print(odct1)
print(odct2)
输出:
OrderedDict([('date', '2021-05-07 01:59:37'), ('Genre', 10), ('action', 'Notify'), ('type', 'Something')])
OrderedDict([('date', '2021-05-07 01:59:37'), ('Genre', 20), ('action', 'Notify'), ('type', 'Something Else')])
免责声明:这里假设您要处理的每个字典都具有 order
中的所有键。此解决方案适用于任何具有 collections.OrderedDict
的 python 版本,如果您将单独使用 python3.7
或更新版本,您可以使用如下通用 dict
order = ['date', 'Genre', 'action', 'type']
dct1 = dict.fromkeys(order)
dct1.update({'action': 'Notify', 'type': 'Something', 'Genre': 10, 'date': '2021-05-07 01:59:37'})
print(dct1)
输出
{'date': '2021-05-07 01:59:37', 'Genre': 10, 'action': 'Notify', 'type': 'Something'}
免责声明仍然有效
有了列表理解:
lis = [
{'action': 'Notify', 'type': 'Something', 'Genre': 10, 'date': '2021-05-07 01:59:37'},
{'action': 'Notify', 'type': 'Something Else', 'Genre': 20, 'date': '2021-05-07 01:59:37'}
]
mapping = {1:'date', 2:'Genre', 3:'action', 4:'type'}
sorted_lis = [
{field: record[field] for field in mapping.values()}
for record in lis
]
print(sorted_lis)
试试这个:
def sort_dct(li, mapping):
return {v: li[v] for k,v in mapping.items()}
out = []
mapping = {1:'date', 2:'Genre', 3:'action', 4:'type'}
for li in lis:
out.append(sort_dct(li,mapping))
print(out)
输出:
[{'date': '2021-05-07 01:59:37',
'Genre': 10,
'action': 'Notify',
'type': 'Something'},
{'date': '2021-05-07 01:59:37',
'Genre': 20,
'action': 'Notify',
'type': 'Something Else'}]
所以,我在 python 中有一个字典列表,如下所示:
lis =
[
{'action': 'Notify', 'type': 'Something', 'Genre': 10, 'date': '2021-05-07 01:59:37'},
{'action': 'Notify', 'type': 'Something Else', 'Genre': 20, 'date': '2021-05-07 01:59:37'}
...
]
现在我希望 lis
在某种程度上,这样 每个单独的字典 都使用 mapping
我将提供的键来排序.例如,如果
mapping = {1:'date', 2:'Genre', 3:'action', 4:'type'}
然后,我想让我原来的字典列表看起来像这样:
lis =
[
{'date': '2021-05-07 01:59:37', 'Genre': 10, 'action': 'Notify', 'type': 'Something'},
{'date': '2021-05-07 01:59:37', 'Genre': 20, 'action': 'Notify', 'type': 'Something Else'}
...
]
我该如何实施?
您可以按如下方式利用 collections.OrderedDict
完成此任务
import collections
order = ['date', 'Genre', 'action', 'type']
dct1 = {'action': 'Notify', 'type': 'Something', 'Genre': 10, 'date': '2021-05-07 01:59:37'}
dct2 = {'action': 'Notify', 'type': 'Something Else', 'Genre': 20, 'date': '2021-05-07 01:59:37'}
odct1 = collections.OrderedDict.fromkeys(order)
odct1.update(dct1)
odct2 = collections.OrderedDict.fromkeys(order)
odct2.update(dct2)
print(odct1)
print(odct2)
输出:
OrderedDict([('date', '2021-05-07 01:59:37'), ('Genre', 10), ('action', 'Notify'), ('type', 'Something')])
OrderedDict([('date', '2021-05-07 01:59:37'), ('Genre', 20), ('action', 'Notify'), ('type', 'Something Else')])
免责声明:这里假设您要处理的每个字典都具有 order
中的所有键。此解决方案适用于任何具有 collections.OrderedDict
的 python 版本,如果您将单独使用 python3.7
或更新版本,您可以使用如下通用 dict
order = ['date', 'Genre', 'action', 'type']
dct1 = dict.fromkeys(order)
dct1.update({'action': 'Notify', 'type': 'Something', 'Genre': 10, 'date': '2021-05-07 01:59:37'})
print(dct1)
输出
{'date': '2021-05-07 01:59:37', 'Genre': 10, 'action': 'Notify', 'type': 'Something'}
免责声明仍然有效
有了列表理解:
lis = [
{'action': 'Notify', 'type': 'Something', 'Genre': 10, 'date': '2021-05-07 01:59:37'},
{'action': 'Notify', 'type': 'Something Else', 'Genre': 20, 'date': '2021-05-07 01:59:37'}
]
mapping = {1:'date', 2:'Genre', 3:'action', 4:'type'}
sorted_lis = [
{field: record[field] for field in mapping.values()}
for record in lis
]
print(sorted_lis)
试试这个:
def sort_dct(li, mapping):
return {v: li[v] for k,v in mapping.items()}
out = []
mapping = {1:'date', 2:'Genre', 3:'action', 4:'type'}
for li in lis:
out.append(sort_dct(li,mapping))
print(out)
输出:
[{'date': '2021-05-07 01:59:37',
'Genre': 10,
'action': 'Notify',
'type': 'Something'},
{'date': '2021-05-07 01:59:37',
'Genre': 20,
'action': 'Notify',
'type': 'Something Else'}]