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 "<stdin>", line 1, in <module>
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 "<stdin>", line 1, in <module>
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