Skip to content Skip to sidebar Skip to footer

Is There A Way To Have A Dictionary Key Be A Range?

Forgive me if this is obvious, but I'm very, very new to Python. I've found ways to get multiple keys from a dictionary, but that's not what I'm trying to do. Basically I'm lookin

Solution 1:

I must say I've never had any need to do anything like this, and there's certainly no built-in datastructure for it. (If you know anything about hashes, you'll understand why a dict can't work that way.)

One possibility would be not to use a dict at all, but have separate lists of keys and values, with the key list being the beginning of each "range". So:

keys = [0, 10, 20, 30]
values = ['foo', 'bar', 'baz', 'quux']

And now you can use bisect to find the relevant key:

import bisect
pos = bisect.bisect_left(keys, 12)
value = values[pos-1]

Solution 2:

How about this:

deffancy_dict(*args):
    'Pass in a list of tuples, which will be key/value pairs'
    ret = {}
    for k,v in args:
        for i in k:
            ret[i] = v
    return ret

Then, you can:

>>>dic = fancy_dict((range(10), 'hello'), (range(100,125), 'bye'))>>>dic[1]
'hello'
>>>dic[9]
'hello'
>>>dic[100]
'bye'
>>>

You can also add logic inside of fancy_dict to say, check if an item is a string or if it is iterable and create the dictionary accordingly.

Solution 3:

This certainly is not a common case, i recommend to use the obvious solution:

my_dict = dict((i, "foo") for i inrange(1,10))
print my_dict
{1: 'foo', 2: 'foo', 3: 'foo', 4: 'foo', 5: 'foo', 6: 'foo', 7: 'foo', 8: 'foo', 9: 'foo'}

In order to append new elements you can update your dictionary with:

my_dict.update(new_elements) 

Solution 4:

If your "range keys" are simple mathematical transformations with unique mappings for every potential valid key, you could just subclass list and override __getitem__ and __setitem__, though there's good reasons to just use helper methods or straight calculations in your calling code (such as having index() return something particularly meaningful).

classRangeList(list):def__getitem__(self, index):
        returnsuper(RangeList, self).__getitem__(index / 10if index else0)
    def__setitem__(self, index, value):
        super(RangeList, self).__setitem__(index / 10if index else0, value)

Solution 5:

I keep this for record and may others interest:

It works if you make keys tuple: my_dict = {(1, 2, 3, 10): "foo"}

Edit: I thought you want a list as key. Otherwise, you need make it:

>>>import numpy as np>>>keys = np.arange(10,dtype=int)>>>values = np.arange(3,13)>>>d = dict(numpy.array([keys,values]).T)>>>d
{0: 3, 1: 4, 2: 5, 3: 6, 4: 7, 5: 8, 6: 9, 7: 10, 8: 11, 9: 12}

Post a Comment for "Is There A Way To Have A Dictionary Key Be A Range?"