Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python dictionary search values for keys using regular expression

Tags:

python

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 
like image 646
Programmer Avatar asked May 29 '12 08:05

Programmer


People also ask

How do you search a key and value in a dictionary?

Method 3: Get the key by value using dict. We can also fetch the key from a value by matching all the values using the dict. item() and then print the corresponding key to the given value.

Can you use regex in dictionary Python?

Sure. Just look them up as normal and check for matches. Note that re. match only produces a match if the expression is found at the beginning of the string.

Can we search in values of dictionary Python?

In this Program, we will discuss how to find the value by key in Python dictionary. By using the dict. get() function, we can easily get the value by given key from the dictionary. This method will check the condition if the key is not found then it will return none value and if it is given then it specified the value.

How do I get a list of key values in a dictionary?

keys() function to make your code more explicit. To get a list of the dictionary's values, you can call the dict. values() function.


2 Answers

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.

like image 68
Cédric Julien Avatar answered Sep 25 '22 06:09

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 
like image 23
Andrew Kesterson Avatar answered Sep 24 '22 06:09

Andrew Kesterson