Accessing Dictionary values from within the Dictionary 
Author Message
 Accessing Dictionary values from within the Dictionary

Hi again,

I have created a dictionary, like so:

dict = {var1 : 0, var 2: 0}

Now, i want the third key/value pair to equal the total values of var1 and
var2. I tried to do it this way:

dict = {var1 : 1, var 2 : 2, var3 : dict[var1] + dict[var2]}

But I get an invalid syntax error.

Could someone help me out with this?

Thanks,
Kuros



Tue, 03 May 2005 13:57:33 GMT  
 Accessing Dictionary values from within the Dictionary


Quote:
>Hi again,

>I have created a dictionary, like so:

>dict = {var1 : 0, var 2: 0}

>Now, i want the third key/value pair to equal the total values of var1 and
>var2. I tried to do it this way:

>dict = {var1 : 1, var 2 : 2, var3 : dict[var1] + dict[var2]}

>But I get an invalid syntax error.

>Could someone help me out with this?

>Thanks,
>Kuros

two statements will work
dict = {var1: 0, var2: 0}
dict[var3] = dict[var1] + dict[var2]

--
Robin Becker



Tue, 03 May 2005 20:17:41 GMT  
 Accessing Dictionary values from within the Dictionary

Quote:

> Hi again,

> I have created a dictionary, like so:

> dict = {var1 : 0, var 2: 0}

> Now, i want the third key/value pair to equal the total values of var1 and
> var2. I tried to do it this way:

> dict = {var1 : 1, var 2 : 2, var3 : dict[var1] + dict[var2]}

> But I get an invalid syntax error.

> Could someone help me out with this?

The main help I can give you: copy and paste the example you say
is failing -- make it as tiny as possible as long as it still fails.
When you try to teport an error WITHOUT copy and paste, it's just
too likely that what you end up saying is just false, thus wasting
the time of anybody who's trying to help.

E.g, in this case:

Quote:
>>> dict = {var1 : 0, var 2: 0}

  File "<stdin>", line 1
    dict = {var1 : 0, var 2: 0}
                          ^
SyntaxError: invalid syntax

So you CANNOT possibly, as you claim, have created a dictionary
as you say -- that weird extra space in 'var 2' makes your
assertion impossible.

Another piece of advice: don't name your variables dict, list,
tuple, file, int, str, and by other built-in type names used by
Python.  It's not illegal, but it makes for a LOT of confusion
and needless bugs.  Call them adict, somelist, mytuple, thefile,
etc, if it's just TOO hard to think of a useful name...

Alex



Tue, 03 May 2005 21:08:26 GMT  
 Accessing Dictionary values from within the Dictionary
[Kuros]

Quote:
> I have created a dictionary, like so:
> dict = {var1 : 0, var 2: 0}

Did you mean?

    dict = {'var1': 0, 'var2': 0}

Be careful, you probably did not intend a space between `var' and `2'.

Quote:
> dict = {var1 : 1, var 2 : 2, var3 : dict[var1] + dict[var2]}
> But I get an invalid syntax error.

Probably this would work for you:

    dict = {'var1': 1, 'var2': 2, 'var3': dict['var1'] + dict['var2']}

But you know, you may add to a dictionary without rebuilding whole.
After having done:

    dict = {'var1': 0, 'var2': 0}

you may well merely complete it by doing only:

    dict['var3'] = dict['var1'] + dict['var2']

Quote:
> Could someone help me out with this?

The best advice I think I could give you is developping the habit of
re-reading your code very carefully.  Hmph!  I hope I do not myself have
too many mistakes in this reply :-).

Programming languages are not much forgiving, you have to be extremely
precise in what you write.  It should also be worth going through the
python tutorial very patiently, repeating all the exercises by yourself,
playing around them, mastering everything written in there.  You might
also choose to buy good books on Python programming, there are many, and
accepting to invest time for scrutinising them carefully.  These should
help you getting acquainted with the art of programming!

--
Fran?ois Pinard   http://www.iro.umontreal.ca/~pinard



Tue, 03 May 2005 21:13:59 GMT  
 Accessing Dictionary values from within the Dictionary

Quote:

>Probably this would work for you:

>    dict = {'var1': 1, 'var2': 2, 'var3': dict['var1'] + dict['var2']}

I doubt it:

Python 2.1.3 (#35, Apr  8 2002, 17:47:50) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.

Quote:
>>> dict = {'var1': 1, 'var2': 2, 'var3': dict['var1'] + dict['var2']}

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name 'dict' is not defined

   If the original poster had said *which* syntax error..

To the original poster:

   Consider just

dict = {'var1':1, 'var2':2}

   First the Python interpreter builds a dictionary using
the expression `{'var1':1, 'var2':2}`

   If this succeeds, then the interpreter associates the
dictionary object with the name `dict` in one of its own
symbol dictionaries.  So when you try to refer to the `dict`
dictionary within the expression that builds the dictionary
that later will be called `dict`.. you find there is no such
thing.

.. if you're lucky.  If there's a different object called
`dict` at the time your statement is executed, that will be
used instead.  That had better be what you want.

   It goes beyond Python .. most common computer languages
.. certainly the fortran- or Algol-flavoured ones ,, work
this way.

        Regards.        Mel.



Tue, 03 May 2005 23:10:39 GMT  
 Accessing Dictionary values from within the Dictionary

Quote:

> Hi again,

> I have created a dictionary, like so:

> dict = {var1 : 0, var 2: 0}

> Now, i want the third key/value pair to equal the total values of var1 and
> var2. I tried to do it this way:

> dict = {var1 : 1, var 2 : 2, var3 : dict[var1] + dict[var2]}

> But I get an invalid syntax error.

> Could someone help me out with this?

Others have posted code that will work, so I will limit myself to one
code-related comment: don't use dict as a variable name, *ever*. Nor
int, str, list, tuple... Those are not reserved words, so Python lets
you use them as variable names -- but by doing so, you're shadowing a
built-in function, which is a Bad Idea and will cause much confusion
(and subtle bugs) later on when you're not expecting problems.

Now, the concept you need to understand here is precisely how assignment
works: *first* the right-hand side is evaluated, and only *after* that
does the result get bound to the name on the left-hand side. So in your
example, at the time that "dict[var1]" and "dict[var2]" get evaluated,
there is no dictionary named dict and so these expressions fail. But
the following code will work:

the_dict = {'var1': 1, 'var2': 2}
the_dict['var3'] = the_dict['var1'] + the_dict['var2']

When the RHS of the second statement is evaluated, the name the_dict is
already bound to a dictionary object that has keys 'var1' and 'var2'.
(BTW, make *sure* you understand the difference between "dict[var1]" and
"dict['var1']"! If you don't understand the difference between those two
expressions, you *will* have problems.) So the expression
"the_dict['var1'] + the_dict['var2']" works and evaluates to 3; then 3
is stored in the_dict['var3']. Note that this does *not* work like a
spreadsheet: if you later change the_dict['var1'], the_dict['var3'] will
*not* be recomputed.

Hope this helps you understand this better.

--

http://www.rmunn.com/
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838



Thu, 05 May 2005 15:44:55 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Dictionary of dictionaries

2. Dictionary to string and back to dictionary??

3. losrt -dictionary and dictionary sorting...

4. How to declare a TYPE within the dictionary

5. problem with dictionaries within lists

6. Searching dictionaries for large integer values

7. Check Box Properties in Dictionary does not retain True/False value

8. Cookbook: Associating multiple values with each key in a dictionary

9. Linking Dictionary Values

10. Confusion about dictionaries - keys use value or identity?

11. sort dictionary by value

12. dictionary sub-value lookup

 

 
Powered by phpBB® Forum Software