1

I have a fixed dictionary which I cannot change and need to find the correct key without knowing the number of whitespace in the keys. Here is what I have:

mydict = dict(
    'entry  1'='first',
    'entry  2'='second',
    # ...
    'entry 10'='tenth',
    # ...
)

I need to find the correct entry based on the "number", e.g. mydict[entry 4], but without knowing how much whitespace separates the word entry and the number. Any idea how I can achieve this?

I would assume there is some clever regex I can use that allows for an arbitrary amount of characters between entry and the number, but with whitespace as the only allowed character.

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Short answer: No, you cannot query the dictionary without knowing the exact key (and keep the O(1) query capabilities.

Option1 Uses same space, but takes leniar time, and treats your dictionary as a list

for k, v in mydict.items():
    if k.startswith("entry") and k.endswith(4): ## or use a regex pattern here
        return v

Option2 use an auxiliary datastructure that keeps track of changes to mydict and has better whitespace insensitive keys. Uses proportionally more memory, but you get the advantages of the dictionary

mydict_index = {}
for k, v in mydict.items():
    index_key = k.replace(" ", "")
    mydict_index[index_key] = v

Note that there are fancier ways of doing option 2, and you can hide the fact that you're using an auxiliary datastructure by wrapping your dictionary in one.

Comments

0

Use the following regexp:

import re

my_number = 14

pattern = r"entry\s+{}".format(my_number)

Then test your keys with some match or search function of the re package.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.