Python dictionary search values for keys using regular expression
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
.
how to get dict value by regex in python
>>> [x for d in dict1 for x in dict1[d] if d.startswith("s")]
[1, 2, 3, 4, 5, 6, 10, 11]
or, if it needs to be a regex
>>> regex = re.compile("^s")
>>> [x for d in dict1 for x in dict1[d] if regex.search(d)]
[1, 2, 3, 4, 5, 6, 10, 11]
What you're seeing here is a nested list comprehension. It's equivalent to
result = []
for d in dict1:
for x in dict1[d]:
if regex.search(d):
result.append(x)
As such, it's a little inefficient because the regex is tested way too often (and the elements are appended one by one). So another solution would be
result = []
for d in dict1:
if regex.search(d):
result.extend(dict1[d])
Finding key in python dictionary using regex
If you must use regular expressions, you can use re.match
while iterating over the dictionary.
import re
dictionary = {'Storgatan': 46, 'Talgvägen': 51, 'Malmstigen': 8, 'Huvudgatan': 3}
regex = '.*gatan$'
results = [v for k, v in dictionary.items() if re.match(regex, k)]
print(results)
Output:
[46, 3]
N.B: This'll be slow for large dictionaries
If you want just the key names:
matching_keys = [k for k in dictionary if re.match(regex, k)]
Python - Find key in dictionary using regex
This regex solution should work for you.
import re
# fixed pre-given dictionary
mydict ={
'entry 1':'first',
'entry 2':'second',
'entry 4' : 'fourth',
'entry 10':'tenth'}
# the number that you have been given; in your example, 4
num = 4
#loop through the keys and values of the dictionary
for key, val in mydict.items():
# use regex to find all integers in the key. Ignores the whitespace
ints = list(map(int, re.findall(r'\d+', key)))
# if integer in the key, is the number that you have been given
if num in ints:
# then you have found your key and value
print(f'key: {key}\nval: {val}')
It loops through every key, value pair in the dictionary and finds the key that ends with the desired number.
Insert values in dictionary using regex which includes key in the pattern
You could use
import re
data = """
Name : Luke Cameron
Age and Sex : 37/Male
Haemoglobin 13.0 g/dL"""
rx = re.compile(r'^(?P<key>[^:\n]+):(?P<value>.+)', re.M)
result = {}
for match in rx.finditer(data):
key = match.group('key').rstrip()
value = match.group('value').strip()
try:
key1, key2 = key.split(" and ")
value1, value2 = value.split("/")
result.update({key1: value1, key2: value2})
except ValueError:
result.update({key: value})
print(result)
Which yields
{'Name': 'Luke Cameron', 'Age': '37', 'Sex': 'Male'}
Iterating Through Dictionary To Get Value Using Regex
I think this might help
banks = [{'bank_code': 'BANKA',
'profit_rate': 2.7},
{'bank_code': 'BANKB',
'interest_rate': 2.6}]
for bank in banks:
x = list(bank.keys())[1]
print(bank[x])
Python: How to sort dictionary value using regex
The following code is one way of producing the results you're asking for using a dictionary. The only catch is that since a dictionary doesn't allow duplicate keys, you need the dictionary values to be lists (containing the duplicate key values).
Please note I had to change tab character to |||
since repl.it isn't allowing me to use the tab character in files. You would have to replace |||
with \t
in the script below.
See it run here
import glob
from collections import defaultdict, OrderedDict
d = defaultdict(list)
def read():
for files in glob.glob('*.txt'):
with open(files, 'r') as f:
for line in f:
r = line.rstrip('\n').split('|||')
d[r[2]].append(r)
def display(d):
od = OrderedDict(sorted(d.items(), reverse=True))
for k,v in od.items(): print(k,v)
read()
display(d)
The output for the script above is below:
900 [['file', 'GameOfThrones', '900', '0']]
504 [['file', 'DC/Batman', '504', '1']]
444 [['file', 'Science/Chemistry', '444', '1']]
342 [['file', 'Marvel/CaptainAmerica', '342', '0'], ['file', 'Math/Calculus', '342', '0']]
324 [['file', 'Psychology', '324', '1']]
300 [['file', 'Marvel/GuardiansOfGalaxy', '300', '1']]
234 [['file', 'Anthropology', '234', '0']]
200 [['file', 'DC/Superman', '200', '1'], ['file', 'Science/Biology', '200', '1']]
Different Outputs
Changing print(k,v)
to another format gets you different output as you've noted in the comments below. For example:
print(k, [x[1] for x in v])
This results in the following:
900 ['GameOfThrones']
504 ['DC/Batman']
444 ['Science/Chemistry']
342 ['Marvel/CaptainAmerica', 'Math/Calculus']
324 ['Psychology']
300 ['Marvel/GuardiansOfGalaxy']
234 ['Anthropology']
200 ['DC/Superman', 'Science/Biology']
Python : find and replace patterns in the value of dictionary that is a list of strings
replace()
is an inbuilt function in Python programming language that returns a copy of the string where all occurrences of a substring is replaced with another substring.
Ex.
dictionarylst = {0:["example inside some sentence", "something else", "some
blah"], 1:["testing", "some other word"],2:["a new expression",
"my cat is cute"]}
wordslist = ["expression 1", "my expression", "other", "blah"]
dictionarycleaned = {}
def match_pattern(wordslist,value):
new_list = []
for text in value:
# temp variable hold latest updated text
temp = text
for word in wordslist:
if word in text:
# replace text string with whitespace if word in text
temp = temp.replace(word,"")
new_list.append(temp)
return new_list
for k,v in dictionarylst.items():
dictionarycleaned[k] = match_pattern(wordslist, v)
print(dictionarycleaned)
O/P:
{0: ['example inside some sentence', 'something else', 'some '], 1: ['testing', 'some
word'], 2: ['a new expression', 'my cat is cute']}
Related Topics
How to Clear Your Printed Text in Python
Regular Expression: Match Everything After a Particular Word
How to Reset Anaconda Root Environment
How to Remove the Decimal Point in a Pandas Dataframe
Pandas Populate New Dataframe Column Based on Matching Columns in Another Dataframe
Converting Pandas Column of Comma-Separated Strings into Integers
How to Specify File Path in Jupyter Notebook
Fast Way to Split Column into Multiple Rows in Pandas
How to Count the Total Number of Words in a Pandas Dataframe Cell and Add Those to a New Column
How to Check All Versions of Python Installed on Osx and Centos
Receiving Integers from the User Until They Enter 0
How to Get Max Output from a While Loop
Saving the Output of a Python Program
How to Remove All Characters Before a Specific Character in Python
Export Pandas Dataframe into a Pdf File Using Python
How to Overwrite Part of a Text File in Python
Split/Explode a Column of Dictionaries into Separate Columns With Pandas