简体   繁体   中英

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. 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. 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".
  2. Apply ordering to dictionary items . Dictionaries are unordered in Python (unless you are using 3.7+).
  3. Use a comprehension with enumerate to construct your list result.

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%)'}}]

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