简体   繁体   English

将Dict对象转换为dict对象列表

[英]Conversion of Dict object into a list of dict object

I have the following dict structure 我有以下字典结构

d = {'Attributes': {'Fifth': 'blind (19.33%)',
                    'First': 'Art (40.0%)',
                    'Fourth': 'Ser (20.0%)',
                    'Second': 'Nat (21.33%)',
                    'Third': 'per (20.67%)'}}

Need to convert into the following structure list of dictionary items 需要转换成以下字典项目的结构清单

 [   0: {'First': 'Art (40.0%)'},
     1: {'Second': 'Nat (21.33%)'},
     2: {'Third': 'per (20.67%)'},
     3: {'Fourth': 'Ser (20.0%)'},
     4: {'Fifth': 'blind (19.33%)'}
 ]

First of all, the structure you want as output is not a python list format. 首先,您要输出的结构不是python list格式。 Actually, it is not also a dictionary format either. 实际上,它也不是字典格式。

From your question, I've learned that you want to make a list of dictionaries. 从您的问题中,我了解到您想列出字典。

First, make a dictionary element: 首先,制作一个字典元素:

0: {'First': 'Art (40.0%)'}

to

{0: {'First': 'Art (40.0%)'}}

Then, you will be ready to make a list of a dictionary and your data structure will look like: 然后,您将准备列出字典,您的数据结构将如下所示:

[   {0: {'First': 'Art (40.0%)'}},
     {1: {'Second': 'Nat (21.33%)'}},
     {2: {'Third': 'per (20.67%)'}},
     {3: {'Fourth': 'Ser (20.0%)'}},
     {4: {'Fifth': 'blind (19.33%)'}}
 ]

you can check the structure: 您可以检查结构:

list =  [   {0: {'First': 'Art (40.0%)'}},
     {1: {'Second': 'Nat (21.33%)'}},
     {2: {'Third': 'per (20.67%)'}},
     {3: {'Fourth': 'Ser (20.0%)'}},
     {4: {'Fifth': 'blind (19.33%)'}}
 ]
print(type(a))
print(type(list[0]))

Output: 输出:

<class 'list'>
<class 'dict'>

And the code 和代码

dict_value = {'Attributes': {'Fifth': 'blind (19.33%)',
                    'First': 'Art (40.0%)',
                    'Fourth': 'Ser (20.0%)',
                    'Second': 'Nat (21.33%)',
                    'Third': 'per (20.67%)'}}

order = {value: key for key, value in enumerate(('First', 'Second', 'Third', 'Fourth', 'Fifth'))}

sorted_form = sorted(dict_value['Attributes'].items(), key=lambda d: order[d[0]])
final_list = [dict(enumerate({key: value} for key, value in sorted_form))]

print(final_list)

produces 产生

[{0: {'First': 'Art (40.0%)'}, 1: {'Second': 'Nat (21.33%)'}, 2: {'Third': 'per (20.67%)'}, 3: {'Fourth': 'Ser (20.0%)'}, 4: {'Fifth': 'blind (19.33%)'}}]

Your question is unclear and your desired output is not valid Python. 您的问题不清楚,并且所需的输出无效的Python。 I assume you want a list of dictionaries as your desired output. 我假设您要使用词典列表作为所需的输出。 There are a few steps. 有几个步骤。

  1. Define your order . 定义您的订单 Python doesn't know the string "Fourth" should come after "Third". Python不知道字符串“ Fourth”应在“ Third”之后。
  2. Apply ordering to dictionary items . 对字典项应用排序 Dictionaries are unordered in Python (unless you are using 3.7+). 字典在Python中是无序的(除非您使用的是3.7+)。
  3. Use a comprehension with enumerate to construct your list result. 对enumerate使用理解可构造列表结果。

Here's a complete example. 这是一个完整的例子。

d = {'Attributes': {'Fifth': 'blind (19.33%)',
                    'First': 'Art (40.0%)',
                    'Fourth': 'Ser (20.0%)',
                    'Second': 'Nat (21.33%)',
                    'Third': 'per (20.67%)'}}

order = {v: k for k, v in enumerate(('First', 'Second', 'Third', 'Fourth', 'Fifth'))}

sorter = sorted(d['Attributes'].items(), key=lambda x: order[x[0]])

L = [dict(enumerate({k: v} for k, v in sorter))]

print(L)

[{0: {'First': 'Art (40.0%)'},
  1: {'Second': 'Nat (21.33%)'},
  2: {'Third': 'per (20.67%)'},
  3: {'Fourth': 'Ser (20.0%)'},
  4: {'Fifth': 'blind (19.33%)'}}]

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

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