0

I am not even sure if this is possible as I am new to python. Essentially I want to construct a dict for lookup purposes - this is my first stab at a data structure.

lookup = {
    1000000: {
        "Name":"Neutral grassland",
        "values":[6150166, 1000000, 1000283, 1000516, 1000524, 1000525, 1000526, 
                  1300000, 1300016, 1300017, 1300522, 1300527, 1310000, 1320000]
    }
}

I would like a method, a little like get() but where I can return the whole object of 100000 when a number is found within the values array.

Is this possible? Bonkers?

1
  • if my_value in lookup[1000000]["values"]: do_something() Commented Aug 3, 2021 at 12:15

2 Answers 2

1

Is this what you were thinking?

def search(num, lookup):
    for inner in lookup:
        if num in inner["values"]:
            return inner

lookup = {
    1000000: {
        "Name":"Neutral grassland",
        "values":[6150166, 1000000, 1000283, 1000516, 1000524, 1000525, 1000526, 1300000, 1300016, 1300017, 1300522, 1300527, 1310000, 1320000]
    }
}
n = 6150166
search(n, lookup)
Sign up to request clarification or add additional context in comments.

1 Comment

Don't use dict, list, str, etc. as variable names - you're shadowing the builtins.
0

Try this:

def get(v):
    if v in lookup[1000000]["values"]:
        return lookup[1000000]

It's pretty specific to the lookup dictionary, but it can be modified to support others.

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.