python - Assign a Range of Numbers to a Single Key with Dictionaries -


i working on dictionary, came issue. , issue is, want able use more 1 number in order reference key in dictionary.

for example, want range of numbers between 1 , 5 assigned to, let's say, "apple". came this:

my_dict['apple'] = range(1,5) 

at program's current state, far being able run, testing issue, not receive issues editor. wondering, correct way go this? or there better way?

thanks.

edit:

a little more info: want make random integer randint function. then, after python has generated number, want use call key assigned value of random integer. thing is, want make things more common others, want make range of numbers can call larger chance of key coming becomes likelier. sorry if doesn't make sense, @ current state, don't have code show i'm trying accomplish.

you have dictionary backwards. if want able recall, e.g., 'apple' of numbers 1-5, you'd need numbers keys, not values.

for in range(1,6):  # range(a,b) gives [a,b)     my_dict[i] = 'apple' 

etc. then, my_dict[4] == 'apple' , same true other values in range.

this can create large dictionaries many copies of same value.

alternately, can use range objects dictionary keys, testing bit more cumbersome unless create own class.

my_dict[range(1,6)] = 'apple' n = random.randint(1, 5)  key in my_dict:     if n in key:         print(my_dict[key]) 

...prints apple.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -