Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get all keys&values in nested dict of list-of-dicts and dicts?

{'action_name':'mobile signup',
    'functions':[{'name':'test_signUp',
                  'parameters':{'username':'[email protected]',
                                'password':'12345',
                                'mobileLater':'123454231',
                                'mobile':'1e2w1e2w',
                                'card':'1232313',
                                'cardLater':'1234321234321'}}],
    'validations':[
            {'MOB_header':'My stores'},
            {'url':"/stores/my"}]}

I want to get all the keys & values of this dict as a list (out of values that they are dict or array)

print result should be like this:

action name = mobile signup
name = test_signUp
username : [email protected]
password : 12345
mobileLater: 123454231
mobile : 1e2w1e2w
card : 1232313 
cardLater : 1234321234321
MOB_header : My stores
like image 778
eligro Avatar asked May 13 '12 05:05

eligro


People also ask

Which command is used to obtain all the keys in a database?

The Redis KEYS command returns all the keys in the database that match a pattern (or all the keys in the key space). Similar commands for fetching all the fields stored in a hash is HGETALL and for all fetching the members of a SMEMBERS. The keys in Redis themselves are stored in a dictionary (aka a hash table).

How can I see all Redis keys?

To list the keys in the Redis data store, use the KEYS command followed by a specific pattern. Redis will search the keys for all the keys matching the specified pattern. In our example, we can use an asterisk (*) to match all the keys in the data store to get all the keys.

What is Rediss?

What is Redis? Redis, which stands for Remote Dictionary Server, is a fast, open source, in-memory, key-value data store. The project started when Salvatore Sanfilippo, the original developer of Redis, wanted to improve the scalability of his Italian startup.

What is Redis key?

Basic Operations As a dictionary, Redis allows you to set and retrieve pairs of keys and values. Think of a “key” as a unique identifier (string, integer, etc.) and a “value” as whatever data you want to associate with that key. Values can be strings, integers, floats, booleans, binary, lists, arrays, dates, and more.


2 Answers

I have modified a little bit from this link to get all keys&values in nested dict of list-of-dicts and dicts:

def recursive_items(dictionary):
    for key, value in dictionary.items():
        if type(value) is dict:
            yield (key, value)
            yield from recursive_items(value)
        elif type(value) is list:
            yield (key, value)
            for i in value:
                if type(i) is dict:
                    yield from recursive_items(i)
        else:
            yield (key, value)

for i in recursive_items(your_dict):
    print(i) #print out tuple of (key, value)

Output:

('action_name', 'mobile signup')
('functions', [{'name': 'test_signUp', 'parameters': {'username': 
'[email protected]', 'password': '12345', 'mobileLater': '123454231', 'mobile': 
'1e2w1e2w', 'card': '1232313', 'cardLater': '1234321234321'}}])
('name', 'test_signUp')
('parameters', {'username': '[email protected]', 'password': '12345', 
'mobileLater': '123454231', 'mobile': '1e2w1e2w', 'card': '1232313', 
'cardLater': '1234321234321'})
('username', '[email protected]')
('password', '12345')
('mobileLater', '123454231')
('mobile', '1e2w1e2w')
('card', '1232313')
('cardLater', '1234321234321')
('validations', [{'MOB_header': 'My stores'}, {'url': '/stores/my'}])
('MOB_header', 'My stores')
('url', '/stores/my')
like image 106
Anh Khoa Dang Avatar answered Oct 24 '22 10:10

Anh Khoa Dang


You might want to use a recursive function to extract all the key, value pairs.

def extract(dict_in, dict_out):
    for key, value in dict_in.iteritems():
        if isinstance(value, dict): # If value itself is dictionary
            extract(value, dict_out)
        elif isinstance(value, unicode):
            # Write to dict_out
            dict_out[key] = value
    return dict_out

Something of this sort. I come from C++ background so I had to google for all the syntaxes.

like image 33
Hindol Avatar answered Oct 24 '22 11:10

Hindol