Home > front end >  python, randomly get keys from a dictionary based on values (with regex)
python, randomly get keys from a dictionary based on values (with regex)

Time:05-03

I have a dictionary like this,

ex = {
'a': 'tf',
'b': 'tf',
'c': '0fft',
'd': '0tfff',
'e': '0fft', 
'f': 'tft', ...
}

where there are many same values for different keys. The values are combinations of 't', 'f', and '0'.

The tricky things are,

a), '0' is a wildcard and can be regarded as either a 't' or 'f'

b), two continuous 'f' can be regarded as either two 'f's or just one 'f'

I want to get a certain number of random keys from the dictionary based on their values:

def get_random(dictionary, count, feature):
    candidate = [k for k,v in dictionary.items() if v == feature]
    return random.sample(candidate, count)

An expected example: get_random(ex, 3, 'tft') will return ['c', 'e', 'f']. And 'd' ('0tfff') can be selected if the input feature is 'ftfff', 'ttfff', 'ftff', or 'ttff'.

I guess I can use regex to do this, but I have no idea how to implement it into the function.

CodePudding user response:

IIUC you need to craft a regex for each dictionary value and use a loop to test all of them:

def match(s, query):
    import re
    regex = re.sub(r'f ', 'f ', s)
    regex = regex.replace('0', '(t|f )')
    return re.fullmatch(regex, query)
    
query = 'tft'
out = [k for k,s in ex.items() if match(s, query)]

output: ['c', 'e', 'f']

output for query='ftfff' -> ['d']

  • Related