Re: dictionary into desired variable....
On Friday, August 10, 2012 8:31:48 AM UTC-7, Tamer Higazi wrote: > let us say a would be x = [2,5,4] > > y = a[3] > > if I change y to [] > > I want the result to be x = [2,5,[]] and that's automaticly There is no such thing as a[3] if a=[2,4,5]. And this is a list not a dictionary, so I would suggest a tutorial from the Python wiki page. You have to use a mutable container. Integers are not mutable. Lists, for example are. y=[4] x = [2,5,y] print x y[0]=10 print x -- http://mail.python.org/mailman/listinfo/python-list
Re: dictionary into desired variable....
Sorry, I ment of course list what I exaclty ment is that if I assign value = Number that I automaticly assign y[1][3][6][1][1] a new number. more detailled explained: let us say a would be x = [2,5,4] y = a[3] if I change y to [] I want the result to be x = [2,5,[]] and that's automaticly independently how deep i dig. Sorry for the inconvenience. Tamer Am 10.08.2012 16:31, schrieb Chris Angelico: > On Sat, Aug 11, 2012 at 12: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: >> >> y[1][3][6][1][1] >> >> Now, how do I write a python routine, that points in this dictionary, >> where I should receive or set other values. > > For a start, that looks like a list, not a dictionary. A dict in > Python is a mapping, eg a hashtable - an unordered pairing of keys and > values. But this might do what you want: > > value = y > for idx in x: >value = value[idx] > > At the end of that, 'value' will be the same as 'y[1][3][6][1][1]'. > > If that's not what you mean, you may want to clarify your question some. > > Hope that helps! > > Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list
Re: dictionary into desired variable....
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: > > y[1][3][6][1][1] > > 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. -- DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: dictionary into desired variable....
: > suppose you have a dictionary that looks like this: > > x = [1,3,6,1,1] which should represent a certain other variable. That's a list, not a dict. > in reality it would represent: > > y[1][3][6][1][1] > > Now, how do I write a python routine, that points in this dictionary, > where I should receive or set other values. >>> x = [1, 3, 6, 1, 1] >>> y = [0, [0, 1, 2, [0, 1, 2, 3, 4, 5, [0, [0, 'bingo!'] >>> def drill(tree, path): ... target = tree ... for step in path: ... target = target[step] ... return target ... >>> drill(y, x) 'bingo!' >>> -[]z. -- http://mail.python.org/mailman/listinfo/python-list
Re: dictionary into desired variable....
On Sat, Aug 11, 2012 at 12: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: > > y[1][3][6][1][1] > > Now, how do I write a python routine, that points in this dictionary, > where I should receive or set other values. For a start, that looks like a list, not a dictionary. A dict in Python is a mapping, eg a hashtable - an unordered pairing of keys and values. But this might do what you want: value = y for idx in x: value = value[idx] At the end of that, 'value' will be the same as 'y[1][3][6][1][1]'. If that's not what you mean, you may want to clarify your question some. Hope that helps! Chris Angelico -- http://mail.python.org/mailman/listinfo/python-list