Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

dictionary into desired variable....

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


th982a at googlemail

Aug 10, 2012, 7:02 AM

Post #1 of 7 (402 views)
Permalink
dictionary into desired variable....

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.



Tamer

--
http://mail.python.org/mailman/listinfo/python-list


rosuav at gmail

Aug 10, 2012, 7:31 AM

Post #2 of 7 (402 views)
Permalink
Re: dictionary into desired variable.... [In reply to]

On Sat, Aug 11, 2012 at 12:02 AM, Tamer Higazi <th982a [at] googlemail> 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


schesis at gmail

Aug 10, 2012, 7:36 AM

Post #3 of 7 (402 views)
Permalink
Re: dictionary into desired variable.... [In reply to]

:

> 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


d at davea

Aug 10, 2012, 8:14 AM

Post #4 of 7 (403 views)
Permalink
Re: dictionary into desired variable.... [In reply to]

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


th982a at googlemail

Aug 10, 2012, 8:31 AM

Post #5 of 7 (398 views)
Permalink
Re: dictionary into desired variable.... [In reply to]

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 <th982a [at] googlemail> 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


woooee at gmail

Aug 10, 2012, 10:04 AM

Post #6 of 7 (396 views)
Permalink
Re: dictionary into desired variable.... [In reply to]

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


woooee at gmail

Aug 10, 2012, 10:04 AM

Post #7 of 7 (395 views)
Permalink
Re: dictionary into desired variable.... [In reply to]

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

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.