Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python dictionary reference to neighbor dictionary element

I'm trying to create something following:

dictionary = {
   'key1': ['val1','val2'],
   'key2': @key1
}

where @key1 is reference to dictionary['key1']. Thank You in advance.

like image 303
frx Avatar asked Aug 20 '10 13:08

frx


People also ask

How can you access elements from the dictionary?

Accessing Elements from Dictionary Keys can be used either inside square brackets [] or with the get() method. If we use the square brackets [] , KeyError is raised in case a key is not found in the dictionary. On the other hand, the get() method returns None if the key is not found.

Can dictionaries be values in other dictionaries?

But the key values of the dictionary contain different types of values that don't need to maintain any order. When one or more dictionaries are declared inside another dictionary then it is called a nested dictionary or dictionaries of the dictionary.

How do you see if a key exists in a dictionary Python?

Check If Key Exists using has_key() method Using has_key() method returns true if a given key is available in the dictionary, otherwise, it returns a false. With the Inbuilt method has_key(), use the if statement to check if the key is present in the dictionary or not.

How do you connect to a dictionary in Python?

About Dictionaries in Python To create a Dictionary, use {} curly brackets to construct the dictionary and [] square brackets to index it. Separate the key and value with colons : and with commas , between each pair. As with lists we can print out the dictionary by printing the reference to it.


2 Answers

I'd expand Aaron Digulla's version. What he has now is replicated here:

class DictRef(object):
    def __init__(self, d, key): self.d, self.key = d, key

d = {}
d.update({
    'key1': ['val1','val2'],
    'key2': DictRef(d, 'key1')
})

I assume he wishes to replicate true reference semantics, so that accessing 'key2' will always give you a way to get to whatever 'key1' has as a value. the problem is while d['key1'] is a list, d['key2'] is a DictRef. So to get the "value" of 'key1' all you need to do is d['key1'], but for 'key2' you must do d[d['key2'].key]. Furthermore, you can't have a reference to a reference that works sanely, it would become d[d[d['key2'].key].key]. And how do you differentiate between a reference and a regular value? Probably through typechecking with isinstance(v, DictReference) (ew).

So I have a proposal to fix all of this.

Personally, I don't understand why true reference semantics are necessary. If they appear necessary, they can be made unnecessary by restructuring the problem, or the solution to the problem. It may even be that they were never necessary, and a simple d.update(key2=d['key1']) would have solved the issue.

Anyway, to the proposal:

class DictRef(object):
    def __init__(self, d, key): 
        self.d = d
        self.key = key

    def value(self):
        return self.d[self.key].value()

class DictVal(object):
    def __init__(self, value):
        self._value = value

    def value(self):
        return self._value

d = {}
d.update(
    key1=DictVal(['val1', 'val2']),
    key2=DictRef(d, 'key1')
)

Now, in order to get the value of any key, whether it's a reference or not, one would simply do d[k].value(). If there are references to references, they will be dereferenced in a chain correctly. If they form a really long or infinite chain, Python will fail with a RuntimeError for excessive recursion.

like image 124
Devin Jeanpierre Avatar answered Oct 13 '22 18:10

Devin Jeanpierre


Use a new class:

class DictRef(object):
    def __init__(self, d, key): self.d, self.key = d, key

d = {}
d.update({
    'key1': ['val1','val2'],
    'key2': DictRef(d, 'key1')
})

Note the odd syntax because you have no way to know inside of which dictionary you are creating DictRef.

like image 37
Aaron Digulla Avatar answered Oct 13 '22 19:10

Aaron Digulla