Python dictionary search values for keys using regular expression

Question:

I am trying to implement to search for a value in Python dictionary for specific key values (using regular expression as a key).

Example:

I have a Python dictionary which has values like:

{'account_0':123445,'seller_account':454545,'seller_account_0':454676, 'seller_account_number':3433343}

I need to search for values whose key has ‘seller_account’? I wrote a sample program but would like to know if something can be done better. Main reason is I am not sure of regular expression and miss out something (like how do I set re for key starting with ‘seller_account’):

#!usr/bin/python
import re
my_dict={'account_0':123445,'seller_account':454545,'seller_account_0':454676, 'seller_account_number':3433343}

reObj = re.compile('seller_account')

for key in my_dict.keys():
        if(reObj.match(key)):
                print key, my_dict[key]

~ home> python regular.py

seller_account_number 3433343
seller_account_0 454676
seller_account 454545
Asked By: Programmer

||

Answers:

def search(dictionary, substr):
    result = []
    for key in dictionary:
        if substr in key:
            result.append((key, dictionary[key]))   
    return result

>>> my_dict={'account_0':123445,'seller_account':454545,'seller_account_0':454676, 'seller_account_number':3433343}
>>> search(my_dict, 'seller_account')
[('seller_account_number', 3433343), ('seller_account_0', 454676), ('seller_account', 454545)]
Answered By: cval

If you only need to check keys that are starting with "seller_account", you don’t need regex, just use startswith()

my_dict={'account_0':123445,'seller_account':454545,'seller_account_0':454676, 'seller_account_number':3433343}

for key, value in my_dict.iteritems():   # iter on both keys and values
        if key.startswith('seller_account'):
                print key, value

or in a one_liner way :

result = [(key, value) for key, value in my_dict.iteritems() if key.startswith("seller_account")]

NB: for a python 3.X use, replace iteritems() by items() and don’t forget to add () for print.

Answered By: Cédric Julien

You can solve this with dpath.

http://github.com/akesterson/dpath-python

dpath lets you search dictionaries with a glob syntax on the keys, and to filter the values. What you want is trivial:

$ easy_install dpath
>>> dpath.util.search(MY_DICT, 'seller_account*')

… That will return you a big merged dictionary of all the keys matching that glob. If you just want the paths and values:

$ easy_install dpath
>>> for (path, value) in dpath.util.search(MY_DICT, 'seller_account*', yielded=True):
>>> ... # do something with the path and value
Answered By: Andrew Kesterson

You can use a combination of "re" and "filter". for example, if you want to search which methods have the word "stat" in their method name in the os module you can use the code below.

import re 
import os
r = re.compile(".*stat.*")
list(filter(r.match, os.__dict__.keys()))

result is:

['stat', 'lstat', 'fstat', 'fstatvfs', 'statvfs', 'stat_result', 'statvfs_result']

I think the performance issue in the original question is the key_value search after the keys have been found with the "re" module. if a portion of the key is interchangeable we can’t use "startswith". so "re" is a good choice. plus I use a filter to get a list of all matched keys and make a list of them so we can return all values with simple [DICT[k] for k in LIST].

Answered By: Ehsan Ahmadi

like how do I set re for key starting with ‘seller_account’

reObj = re.compile('seller_account')

should be:

reObj = re.compile('seller_account.*')
Answered By: Clintm
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.