简体   繁体   中英

Get Unique list from Json Nested Keys in python

This is my sample Json data.

dict = {'Headers 1': {'sub head 1': { 'birds':['Item 1.1.1',
                                          'Item 1.1.2',
                                          'Item 1.1.3',
                                          'Item 1.1.4'],
                                'animals':['Item 1.1.1',
                                          'Item 1.1.2',
                                          'Item 1.1.3',
                                          'Item 1.1.4']
                                },

               'sub head 2': { 'birds':['Item 1.1.1',
                                          'Item 1.1.2',
                                          'Item 1.1.3',
                                          'Item 1.1.4'],
                                'books':['Item 1.1.1',
                                          'Item 1.1.2',
                                          'Item 1.1.3',
                                          'Item 1.1.4']
                                },
                },
 'Headers 2': {'sub head 1': { 'bottles':['Item 1.1.1',
                                          'Item 1.1.2',
                                          'Item 1.1.3',
                                          'Item 1.1.4'],
                                'animals':['Item 1.1.1',
                                          'Item 1.1.2',
                                          'Item 1.1.3',
                                          'Item 1.1.4']
                                },
               'sub head 2': { 'books':['Item 1.1.1',
                                          'Item 1.1.2',
                                          'Item 1.1.3',
                                          'Item 1.1.4'],
                                'birds':['Item 1.1.1',
                                          'Item 1.1.2',
                                          'Item 1.1.3',
                                          'Item 1.1.4']
                                }
               },
 'Headers 3': {'sub head 1': { 'bottles':['Item 1.1.1',
                                          'Item 1.1.2',
                                          'Item 1.1.3',
                                          'Item 1.1.4'],
                                'birds':['Item 1.1.1',
                                          'Item 1.1.2',
                                          'Item 1.1.3',
                                          'Item 1.1.4']
                                }
                }
 }

Here from this Json data, I want to fetch the unique set of all the Keys of sub headers.My result should be,

['animals','birds','books','bottles']

I will tell my steps that I have done so far,

  1. Get all the Keys of dict. ie

    dict.keys()

  2. Iterate keys one by one get each keys of Parent keys.

  3. Iterate on sub header keyes and get all the child keys of sub headers keys.

Now I don't know how to store this child keys to a unique set in python? How can I that unique list? Could I get the list sorted?

Assuming you want the result unordered , iterate 3 levels deep and take the set() of keys on the third level:

print(set(k for outer in d.values() for inner in outer.values() for k in inner))
# {'birds', 'books', 'animals', 'bottles'}

If you want a list instead, wrap list() :

print(list(set(k for outer in d.values() for inner in outer.values() for k in inner)))
# ['birds', 'books', 'bottles', 'animals']

If you want to have a sorted list, wrap sorted() :

print(sorted(set(k for outer in d.values() for inner in outer.values() for k in inner)))
# ['animals', 'birds', 'books', 'bottles']

If you want an ordered result, keep a seen set to keep track of duplicates and store the result in a list:

seen = set()
result = []

for outer in d.values():
    for inner in outer.values():
        for k in inner:
            if k not in seen:
                seen.add(k)
                result.append(k)

print(result)
# ['birds', 'animals', 'books', 'bottles']

The above also doesn't care about the keys on the first two levels, so you can iterate over the dictionary values() instead.

Note: Naming a variable dict is not a good idea, since it shadows the builtin dict() function.

Can use set . Something like:

res = set()
for v in dict.values():
    for i in v.values():
        res.update(set(i.keys()))

print(res) # {'birds', 'animals', 'books', 'bottles'}

On a personal note,

I find this syntax better read than the comprehension syntax. Her you follow the flow, can easy put a break-point and understand what you are doing..

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