On 08/10/2012 10:02 AM, Tamer Higazi wrote: > Hi!
> suppose you have a dictionary that looks like this:
> x = [1,3,6,1,1] which should represent a certain other variable.
> in reality it would represent:
> Now, how do I write a python routine, that points in this dictionary,
> where I should receive or set other values.
Others have pointed out how you could "receive" a value from that.
Doing a setter would be tricker.
But I'd want to start with the question of just what you're really
looking for. Is this an assignment, so it doesn't have to actually make
sense? If so, could we please have the actual wording. x is not a
dictionary, and Python doesn't have routines nor pointers. Perhaps it's
a generic programming class, and the student gets to choose the language.
If it's indeed intended to model some kind of useful data, could we know
a bit more about the constraints? is y supposed to be a nested list, or
a nested dictioary, or something else? How is this nested list/dict
first created? Do you need to have other parts of the code access the
raw, nested Something. Are there always going to be 5 subscripts, and
are each constrained to be in a fixed range? If it's a nested
dictionary, are the keys always integers?
Since a python function or method can't return a pointer, presumably you
mean for a function or method to fetch or store a value.
Sounds like what you want is a class, where the __init__ method takes a
nested something, y and saves it as an attribute _y. And there are at
least two more methods in that class, fetch() that takes a list of ints,
and returns a value. And store() that takes a list of ints and a value,
and mutates the _y, returning None. The store() method is a good bit
trickier to write, depending on the assumptions above.