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.
Aucun commentaire:
Enregistrer un commentaire