P
Pete Emerson
I've been wrestling with dicts. I hope at the very least what I
discovered helps someone else out, but I'm interested in hearing from
more learned python users.
I found out that adding a two dimensional element without defining
first dimension existing doesn't work:
And through some research, I discovered collections.defaultdict (new
in Python 2.5, FWIW):
Why isn't the behavior of collections.defaultdict the default for a
dict?
Am I just revelling in my bad perl habits by not wanting to declare a
previous level first?
Is this sort of "more rigid" way of doing things common throughout
python, and is it best that I not fight it, but embrace it?
Your thoughts and comments are very much appreciated. I think my brain
already knows some of the answers, but my heart ... well, perl and I
go way back. Loving python so far, though.
Pete
discovered helps someone else out, but I'm interested in hearing from
more learned python users.
I found out that adding a two dimensional element without defining
first dimension existing doesn't work:
Traceback (most recent call last):data = {}
data['one']['two'] = 'three'
{'one': {'two': 'three'}}File said:data['one'] = {}
data['one']['two'] = 'three'
print data
And through some research, I discovered collections.defaultdict (new
in Python 2.5, FWIW):
defaultdict(<type 'dict'>, {'one': {'two': 'three'}})import collections
data = collections.defaultdict(dict)
data['one']['two'] = 'three'
print data
Why isn't the behavior of collections.defaultdict the default for a
dict?
Am I just revelling in my bad perl habits by not wanting to declare a
previous level first?
Is this sort of "more rigid" way of doing things common throughout
python, and is it best that I not fight it, but embrace it?
Your thoughts and comments are very much appreciated. I think my brain
already knows some of the answers, but my heart ... well, perl and I
go way back. Loving python so far, though.
Pete