maxer
maxer

Reputation: 1

get all keys from dictionary between two numbers in python

problem fixed, thanks all who helped! :)

I am trying to get all keys/values in a dictionary between a minimum and maximum number.

I am trying to make a product configurator that advises what products (rail sizes combined etc.) are needed to create a certain sized screen-mount.

I have tried searching for this problem on youtube and stack-overflow and getting very close results but i wasn't sure of how to implement them into my code.

sample code (python 3.7):

lookup = {
    500: '1x 500mm',
    1000: '1x 1000mm',
    1500: '1x 1000mm + 1x 500mm',
    2000: '2x 1000mm',
}

print('minimum length: ', min_length '\n maximum length: ', 
max_length, '\n recommended solution: ')

for (trial) in lookup:
    print(lookup(trial >= min_length and trial <= max_length))

the error message i got was:

Traceback (most recent call last):

print(lookup((trial) >= min_rail_calc and (trial) <= max_rail_calc)) TypeError: 'dict' object is not callable

sorry if this is a noob question i'm only new to python

Upvotes: 0

Views: 847

Answers (3)

ty1
ty1

Reputation: 364

So when you do

for trial in lookup:

You are looping through the keys. If you do:

for trial in lookup:
    print(trial)

you will get

#500
#1000
#1500
#2000

If however, you want to access the values at each, you would:

for trial in lookup:
    print(lookup[trial])

So in this case, the 'trial' variable is iterating over the dictionary's keys. So if we query the dictionary at the iterator, it will return that value.

So for your case, if you want to compare the values, and print them, all you have to do is compare trial against min and max:

for trial in lookup:
    if((trial >= min_length) and (trial <= max_length)):
        print (#print whatever here)

Upvotes: 0

Yoel Nisanov
Yoel Nisanov

Reputation: 1054

There you go (:

lookup = {
    500: '1x 500mm',
    1000: '1x 1000mm',
    1500: '1x 1000mm + 1x 500mm',
    2000: '2x 1000mm',
}
min_length = 300
max_length = 1300
print('minimum length: ', min_length, '\nmaximum length: ', max_length, '\nrecommended solution: ')

for key in lookup.keys():
    if min_length < key < max_length:
        print(lookup[key])

on your print you had a syntax error that I've fixed, also calling object on dict getting done by [] and not () and furthermore, you don't iterate per variable inside a dict since their order doesn't have any meaning, you can iterate on all the keys if you wanna perform iteration on all variables.

Upvotes: 0

micharaze
micharaze

Reputation: 976

The iteration over a dict and the access to it is a little bit different than to a list. Try this:

min_length = 600
max_length = 1600

lookup = {
    500: '1x 500mm',
    1000: '1x 1000mm',
    1500: '1x 1000mm + 1x 500mm',
    2000: '2x 1000mm',
}

for trial, value in lookup.items():
    if trial >= min_length and trial <= max_length:
        print(value)

Output:

1x 1000mm
1x 1000mm + 1x 500mm

Upvotes: 1

Related Questions