-3

Here is my dictionary structure:

{
    "432701228292636694" : {
        "432739261603905537" : {
            "channels" : {
                "LoL Duos" : {
                    "capacity" : 2,
                    "rooms" : [
                        "432741328477093889"
                    ]
                },
                "LoL Quads" : {
                    "capacity" : 4,
                    "rooms" : [
                        "432741635852599297"
                    ]
                },
                "LoL Teams" : {
                    "capacity" : 5,
                    "rooms" : [
                        "467708831695110154"
                    ]
                },
                "LoL Trios" : {
                    "capacity" : 3,
                    "rooms" : [
                        "432741537890304030",
                        "468096902055985152"
                    ]
                }
            },
            "perms" : {
                "453625621604728839" : {
                    "read_messages" : false
                },
                "461654834689474560" : {
                    "read_messages" : false
                }
            }
        },
        "432739461475074049" : {
            "channels" : {
                "FN Duos" : {
                    "capacity" : 2,
                    "rooms" : [
                        "432740789660155904"
                    ]
                },
                "FN Squads" : {
                    "capacity" : 4,
                    "rooms" : [
                        "432740857268142081"
                    ]
                },
                "FN Trios" : {
                    "capacity" : 3,
                    "rooms" : [
                        "467707010746417172"
                    ]
                }
            },
            "perms" : {
                "453625621604728839" : {
                    "read_messages" : false
                },
                "461654872815697931" : {
                    "read_messages" : false
                }
            }
        },
        "436634548051378186" : {
            "channels" : {
                "OW Duos" : {
                    "capacity" : 2,
                    "rooms" : [
                        "436636544229441567"
                    ]
                },
                "OW Quads" : {
                    "capacity" : 4,
                    "rooms" : [
                        "436636615167705089"
                    ]
                },
                "OW Teams" : {
                    "capacity" : 5,
                    "rooms" : [
                        "467707823954984971"
                    ]
                },
                "OW Trios" : {
                    "capacity" : 3,
                    "rooms" : [
                        "436636575036866570"
                    ]
                }
            },
            "perms" : {
                "453625621604728839" : {
                    "read_messages" : false
                },
                "461654908329000972" : {
                    "read_messages" : false
                }
            }
        }
    }
}

What I'm wanting to do is check if a string matches any of the values of any rooms. I've found a really messy way to do it like this:

    for category_id in self.gaming_db[server.id]:
        channel_names = self.gaming_db[server.id][category_id]['channels']
        for channel_name in channel_names:
            room_ids.extend([server.get_channel(x) for x in self.gaming_db[server.id][category_id]['channels'][channel_name]['rooms']])

This is if you assume self.gaming_db is this dictionary. Is there a more Pythonic way to do this? I think it has something to do with list comprehensions using lambda? I really don't understand that much so far.

Jerrybibo
  • 1,315
  • 1
  • 21
  • 27
Craig
  • 563
  • 1
  • 6
  • 18

2 Answers2

0

You can use recursion:

import json
def search(d, _search, _key='rooms'):
  return any(search(b, _search) if isinstance(b, dict) else _search in [[], b][a== _key] 
     for a, b in d.items())

print(search(json.loads(source_dict), '436636544229441567'))

Output:

True
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
-1

Using a recursive generator we can walk down the dictionary, so you may customise how you walk the dictionary based on its structure:

def walk(d):
    """Lazily walk down recursive dictionary."""
    for k,v in d.items():
        if isinstance(v, dict):
             yield from walk(v)
        else:
             yield (k, v)

Then, we can build a search that uses said walker:

def search(d, room_id):
    """Return a bool reprenting if a room contains given id."""
    for k, v in walk(d):
        if k=="rooms" and room_id in v:
            return True
    return False

Now, let's test it:

room_id = "436636575036866570"
result = search(data, room_id)
>>> True
Luca Cappelletti
  • 2,485
  • 20
  • 35