[Tutor] append value to dictionary key

2015-03-06 Thread Chris Stinemetz
I would like to append a value to a dictionary key where there is already a
value. Something like the below:

d = {'name': {'2': 0.0, '7': 10.0, '8': 0.0, '9': 0.0}}
append 10 to d['name']['2']
d = {'name': {'2': 0.0,10, '7': 10.0, '8': 0.0, '9': 0.0}}

When I try to this I get the following error:
>>> d['name']['2'].append(10)
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'float' object has no attribute 'append'

I am obviously doing this wrong. How would I accomplish this?

Thank you in advance.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] append value to dictionary key

2015-03-06 Thread Alan Gauld

On 06/03/15 14:28, Chris Stinemetz wrote:

I would like to append a value to a dictionary key where there is already a
value. Something like the below:

d = {'name': {'2': 0.0, '7': 10.0, '8': 0.0, '9': 0.0}}
append 10 to d['name']['2']
d = {'name': {'2': 0.0,10, '7': 10.0, '8': 0.0, '9': 0.0}}

When I try to this I get the following error:

d['name']['2'].append(10)

Traceback (most recent call last):
   File "", line 1, in 
AttributeError: 'float' object has no attribute 'append'



To append a value you need a list.
So put the values of the dictionary in lists.

d = {'name': {'2': [0.0], '7': [10.0], '8': [0.0], '9': [0.0]}}

Now your

d['name']['2'].append(10)

will work and produce:.

d = {'name': {'2': [0.0, 10], '7': [10.0], '8': [0.0], '9': [0.0]}}

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] append value to dictionary key

2015-03-06 Thread Timo

Op 06-03-15 om 15:28 schreef Chris Stinemetz:

I would like to append a value to a dictionary key where there is already a
value. Something like the below:

d = {'name': {'2': 0.0, '7': 10.0, '8': 0.0, '9': 0.0}}
append 10 to d['name']['2']
d = {'name': {'2': 0.0,10, '7': 10.0, '8': 0.0, '9': 0.0}}

When I try to this I get the following error:

d['name']['2'].append(10)

Traceback (most recent call last):
   File "", line 1, in 
AttributeError: 'float' object has no attribute 'append'
Maybe it's easier to break down your problem and start with a smaller 
example. Remove the dictionary and do the following:

>>> x = 0.0
>>> x.append(10)

What do you expect to happen?

Here's the output:
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'float' object has no attribute 'append'

Exactly the same as yours!

So, a float has no 'append' attribute, but what objects do have that? 
Sounds like you need a list to hold an arbitrary number of items.

>>> l = [0.0]
>>> l
[0.0]
>>> l.append(10)
>>> l
[0.0, 10]


Timo



I am obviously doing this wrong. How would I accomplish this?

Thank you in advance.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Idle - ImportError: No module named numpy

2015-03-06 Thread Markos

Hi,

I'm beginning to study the numpy.

When I open a terminal (Debian Squeeze) and run the python interpreter 
the command "import numpy as np" run without errors.


But when I run the same command on idle3 the following error appears.

>>> import numpy as np
Traceback (most recent call last):
  File "", line 1, in 
import numpy as np
ImportError: No module named numpy

How configure idle to load the numpy module?

Thanks,
Markos
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Idle - ImportError: No module named numpy

2015-03-06 Thread WolfRage

Well on the python interpretor did you use python3 or just python?

On 03/06/2015 01:27 PM, Markos wrote:

Hi,

I'm beginning to study the numpy.

When I open a terminal (Debian Squeeze) and run the python interpreter
the command "import numpy as np" run without errors.

But when I run the same command on idle3 the following error appears.

 >>> import numpy as np
Traceback (most recent call last):
   File "", line 1, in 
 import numpy as np
ImportError: No module named numpy

How configure idle to load the numpy module?

Thanks,
Markos
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Idle - ImportError: No module named numpy

2015-03-06 Thread boB Stepp
On Fri, Mar 6, 2015 at 12:27 PM, Markos  wrote:
> Hi,
>
> I'm beginning to study the numpy.
>
> When I open a terminal (Debian Squeeze) and run the python interpreter the
> command "import numpy as np" run without errors.
>
> But when I run the same command on idle3 the following error appears.
>
 import numpy as np
> Traceback (most recent call last):
>   File "", line 1, in 
> import numpy as np
> ImportError: No module named numpy
>
> How configure idle to load the numpy module?

You don't by chance have Python 2 installed as well as Python 3, where
Python 2 is associated with your terminal session? And your numpy
module is for Python 2, not 3? That is my first thought, but I'm sure
the experts will chime in shortly with a more definitive diagnosis.


-- 
boB
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Idle - ImportError: No module named numpy

2015-03-06 Thread Mark Lawrence

On 07/03/2015 00:42, WolfRage wrote:

Well on the python interpretor did you use python3 or just python?



Please don't top post on this list.  It makes following long threads 
difficult if not impossible, and is basically downright irritating. 
Thanks for listening.


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Idle - ImportError: No module named numpy

2015-03-06 Thread Dave Angel

On 03/06/2015 01:27 PM, Markos wrote:

Hi,

I'm beginning to study the numpy.



And what does this have to do with the
   """Strengths & weaknesses of Python lists compared to
   "old school" arrays [Was "Fixed Vector Array"]"""
thread?  Please don't hijack a thread by replying with an unrelated 
message.  Just start a new one with "Write email" or equivalent. 
Address it to tutor@python.org, and it'll be fine.




When I open a terminal (Debian Squeeze) and run the python interpreter
the command "import numpy as np" run without errors.

But when I run the same command on idle3 the following error appears.

 >>> import numpy as np
Traceback (most recent call last):
   File "", line 1, in 
 import numpy as np
ImportError: No module named numpy

How configure idle to load the numpy module?



As others have said, you probably have a version mismatch.  When you ran 
Python from the terminal, what did you call it?  Which version did you get?


When you ran idle3, you presumably ran some version 3 installation of 
Python.


From each interactive session, you can check the Python version with:

 import sys
 sys.version

Unless they're identical in the two environments you describe, you can't 
assume they'll both have numpy loaded.



--
DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor