# How to say \$a=\$b->{"A"} ||={} in Python?

58 views

### beginner

Aug 16, 2007, 6:35:50 PM8/16/07
to
Hi All.

I'd like to do the following in more succint code:

if k in b:
a=b[k]
else:
a={}
b[k]=a

a['A']=1

In perl it is just one line: \$a=\$b->{"A"} ||={}.

Thanks,
Geoffrey

### Lawrence Oluyede

Aug 16, 2007, 6:43:03 PM8/16/07
to
beginner <zyzh...@gmail.com> wrote:
> I'd like to do the following in more succint code:
>
> if k in b:
> a=b[k]
> else:
> a={}
> b[k]=a

b.setdefault(k, a)

--
Lawrence, oluyede.org - neropercaso.it
"It is difficult to get a man to understand
something when his salary depends on not
understanding it" - Upton Sinclair

### beginner

Aug 16, 2007, 6:52:18 PM8/16/07
to
On Aug 16, 5:43 pm, ra...@dot.com (Lawrence Oluyede) wrote:

I am afraid it is not the same. b.setdefault(k, {}) will always create
an empty dict, even if k is in b, as demonstrated in the below code.

b={}
def f(i):
print "I am evaluated %d" % i
return i

b.setdefault('A', f(1))
b.setdefault('A', f(2))
b

### Carl Banks

Aug 16, 2007, 7:03:40 PM8/16/07
to

Define b as a default dict:

b = defaultdict(dict)
b[k]['A'] = l

Carl Banks

### James Stroud

Aug 16, 2007, 7:21:00 PM8/16/07
to

I'm afraid you've asked a non sequiter:

euler 40% cat test.pl

\$a=\$b->{"A"} ||={} ;
print "\$a\n" ;

\$b->{"B"} = 0 ;
\$a=\$b->{"B"} ||={} ;
print "\$a\n" ;

\$b->{"X"} = 15 ;
\$a=\$b->{"X"} ||={} ;
print "\$a\n" ;

euler 41% perl test.pl
HASH(0x92662a0)
HASH(0x926609c)
15

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

### beginner

Aug 16, 2007, 8:53:19 PM8/16/07
to

It is not supposed to be used this way.
\$b is supposed to be a hash-table of hash-table. If a key exists in
\$b, it points to another hash table. The \$a=\$b->{"A"} ||={} pattern is
useful when you want to add records to the double hash table.

For example, if you have a series of records in the format of (K1, K2,
V), and you want to add them to the double hash-table, you can do
\$a=\$b->{K1} || ={}
\$a->{K2}=V

### Jonathan Gardner

Aug 16, 2007, 9:28:34 PM8/16/07
to
On Aug 16, 3:35 pm, beginner <zyzhu2...@gmail.com> wrote:
>
> In perl it is just one line: \$a=\$b->{"A"} ||={}.
>

a = b.setdefault('A', {})

This combines all two actions together:
- Sets b['A'] to {} if it is not already defined
- Assigns b['A'] to a

http://docs.python.org/lib/typesmapping.html

### Paul McGuire

Aug 16, 2007, 10:01:53 PM8/16/07
to
On Aug 16, 8:28 pm, Jonathan Gardner

No, this has already been proposed and discarded. The OP does NOT
want this, because it always generates an empty {} whether it is
needed or not. Not really a big hardship, but if the default value
were some expensive-to-construct container class, then you would be
creating one every time you wanted to reference a value, on the chance
that the key did not exist.

Carl Banks' post using defaultdict is the correct solution. The
raison d'etre for defaultdict, and the reason that it is the solution
to the OP's question, is that instead of creating a just-in-case
default value every time, the defaultdict itself is constructed with a
factory method of some kind (in practice, it appears that this factory
method is usually the list or dict class constructor). If a reference
to the defaultdict gives a not-yet-existing key, then the factory
method is called to construct the new value, that value is stored in
the dict with the given key, and the value is passed back to the
caller. No instances are created unless they are truly required for
initializing an entry for a never-before-seen key.

-- Paul

### Paul McGuire

Aug 16, 2007, 10:09:39 PM8/16/07
to
> Carl Banks- Hide quoted text -
>
> - Show quoted text -

I think the most direct translation of the OP's example to Python
would be:

# setup b as special flavor of dict, that creates a
# new dict for not-yet-created keys
b = defaultdict(dict)

# actual python impl of \$a=\$b->{k}||={}
a = b[k]

# assign 1 to retrieved/created dict
a['A'] = 1

-- Paul

### James Stroud

Aug 16, 2007, 10:20:04 PM8/16/07
to
beginner wrote:
> On Aug 16, 6:21 pm, James Stroud <jstr...@mbi.ucla.edu> wrote:
>>I'm afraid you've asked a non sequiter:
>>
>>euler 40% cat test.pl
>>
>>\$a=\$b->{"A"} ||={} ;
>>print "\$a\n" ;
>>
>>\$b->{"B"} = 0 ;
>>\$a=\$b->{"B"} ||={} ;
>>print "\$a\n" ;
>>
>>\$b->{"X"} = 15 ;
>>\$a=\$b->{"X"} ||={} ;
>>print "\$a\n" ;
>>
>>euler 41% perl test.pl
>>HASH(0x92662a0)
>>HASH(0x926609c)
>>15
>
> It is not supposed to be used this way.
> \$b is supposed to be a hash-table of hash-table. If a key exists in
> \$b, it points to another hash table. The \$a=\$b->{"A"} ||={} pattern is
> useful when you want to add records to the double hash table.
>
> For example, if you have a series of records in the format of (K1, K2,
> V), and you want to add them to the double hash-table, you can do
> \$a=\$b->{K1} || ={}
> \$a->{K2}=V

That's great, but the perl code you provided does not behave identically
to the python code you provided, so your requirments were not well stated.

(Does my annoyance with perl and its ugly syntax show here?)

### Paul McGuire

Aug 16, 2007, 10:25:03 PM8/16/07
to
> \$a->{K2}=V- Hide quoted text -

>
> - Show quoted text -

I think this demonstrates the Python version of what you describe.

-- Paul

from collections import defaultdict

data = [
('A','B',1), ('A','C',2), ('A','D',3), ('B','A',4),
('B','B',5), ('B','C',6), ('B','D',7),
]

def defaultdictFactory():
return defaultdict(dict)

table = defaultdict(defaultdictFactory)

for k1,k2,v in data:
table[k1][k2] = v

for kk in sorted(table.keys()):
print "-",kk
for jj in sorted(table[kk].keys()):
print " -",jj,table[kk][jj]

prints:

- A
- B 1
- C 2
- D 3
- B
- A 4
- B 5
- C 6
- D 7

### Carsten Haese

Aug 16, 2007, 10:32:19 PM8/16/07
to beginner, pytho...@python.org
On Fri, 2007-08-17 at 00:53 +0000, beginner wrote:
> \$b is supposed to be a hash-table of hash-table. If a key exists in
> \$b, it points to another hash table. The \$a=\$b->{"A"} ||={} pattern is
> useful when you want to add records to the double hash table.
>
> For example, if you have a series of records in the format of (K1, K2,
> V), and you want to add them to the double hash-table, you can do
> \$a=\$b->{K1} || ={}
> \$a->{K2}=V

What is the best solution in Perl need not be the best solution in
Python. In Python you should just use a tuple as your dict key, i.e.
a[k1,k2] = v, unless you have some other constraints you're not telling
us.

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net

### beginner

Aug 16, 2007, 11:15:10 PM8/16/07
to

I use tuples this way all the time. It is indeed very neat. But it is
not a replacement for double hash-table. If I want to retrieve
information just by K1, it is not efficient to index on (K1, K2).

### beginner

Aug 16, 2007, 11:16:32 PM8/16/07
to

It looks like defaultdict is the solution for this kind of thing.
Thanks all for your help.

### James Stroud

Aug 16, 2007, 11:37:57 PM8/16/07
to

Define "efficient". As in typing? Lookup should be the same speed for
all keys because its a hash-table.

### Carsten Haese

Aug 17, 2007, 12:02:11 AM8/17/07
to beginner, pytho...@python.org

If you have to look up all values associates with k1 and any k2, you're right,
that's not efficient. That would fall under "other constraints you're not
telling us." I'm not a mind reader.

-Carsten

### Sébastien Buchoux

Aug 17, 2007, 3:35:47 AM8/17/07
to beginner, pytho...@python.org
beginner a écrit :
One solution I often use in such cases:

try:
a = b[k]
except KeyError: #or except IndexError: if b is a list/tuple and not a dict
a = {}
b[k] = a

a['A'] = 1

Indeed, exceptions are handled faster than "if/else" loops. As it was
mentionned earlier, One neat solution in Perl may not be the perfect one
in Python.

Cheers,

Sébastien

### beginner

Aug 17, 2007, 9:41:57 AM8/17/07
to
> Sébastien- Hide quoted text -

>
> - Show quoted text -

Wow. This solution is interesting. I'll try this. Thanks.

### beginner

Aug 17, 2007, 9:43:00 AM8/17/07
to

Yeah, I should have mentioned that I actually want to group the data
by K1 and then by K2.

### Carl Banks

Aug 17, 2007, 8:02:31 PM8/17/07
to

When I made my response, it occurred to me that Python could be
improved (maybe) if one could overload dict.get() to use a factory,
like so:

b = {}
a = b.get(k,factory=dict)
a['A'] = 1

That's a slight improvement (maybe) over defaultdict since it would
still allow the same dict to have the membership check in other
places. I'm not so sure overloading get to let it modify the dict is
a good idea, though.

Actually, it'd probably be fairly uncontroversial to add a factory
keyword to dict.setdefault(). At least insofar as setdefault is
uncontroversial.

Carl Banks

### Steve Holden

Aug 17, 2007, 8:42:28 PM8/17/07
to pytho...@python.org
Well it's uncontroversial enough to have made it into the distribution,
which represents a tacit admission by Guido that the .get() method alone
didn't implement his full vision.

The python-dev thread that discussed the feature before implementation
(as so often is the case) exercised many of the possible design paths,
and would be a useful read. [Damn, now I have to
well-known-search-engine it]. Aah, right - I'd forgotten how long it
took to get it right. This would be a suitable starting-point - it's the
beginning of the /third/ round of discussion:

Many alternatives were discussed, and my memory at this distance is that
Guido had good reasons for choosing the exact API he did for defaultdict.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

### Nick Craig-Wood

Aug 18, 2007, 6:30:06 AM8/18/07
to
beginner <zyzh...@gmail.com> wrote:
> On Aug 16, 5:43 pm, ra...@dot.com (Lawrence Oluyede) wrote:
> > beginner <zyzhu2...@gmail.com> wrote:
> > > I'd like to do the following in more succint code:
> >
> > > if k in b:
> > > a=b[k]
> > > else:
> > > a={}
> > > b[k]=a
> >
> > b.setdefault(k, a)
> >
>
> I am afraid it is not the same. b.setdefault(k, {}) will always create
> an empty dict, even if k is in b

That is certainly true, but does it matter? You waste a very small
amount of time creating a dict you don't use.

\$ python -m timeit '{}'
1000000 loops, best of 3: 0.247 usec per loop

On my machine 250 ns gets you a new dict...

--
Nick Craig-Wood <ni...@craig-wood.com> -- http://www.craig-wood.com/nick

### Nick Craig-Wood

Aug 18, 2007, 7:30:05 AM8/18/07
to
Paul McGuire <pt...@austin.rr.com> wrote:
> Carl Banks' post using defaultdict is the correct solution. The
> raison d'etre for defaultdict, and the reason that it is the solution
> to the OP's question, is that instead of creating a just-in-case
> default value every time, the defaultdict itself is constructed with a
> factory method of some kind (in practice, it appears that this factory
> method is usually the list or dict class constructor). If a reference
> to the defaultdict gives a not-yet-existing key, then the factory
> method is called to construct the new value, that value is stored in
> the dict with the given key, and the value is passed back to the
> caller. No instances are created unless they are truly required for
> initializing an entry for a never-before-seen key.

I think that if you truly want to emulate a perl hash then you would
want this which does the above but recursively.

from collections import defaultdict

class hash(defaultdict):
def __init__(self):
defaultdict.__init__(self, hash)

D=hash()

D[1][2][3][4]=5
D[1][4][5]=6

print D

Aug 18, 2007, 8:41:20 AM8/18/07
to
On Aug 18, 12:30 pm, Nick Craig-Wood <n...@craig-wood.com> wrote:
> I think that if you truly want to emulate a perl hash then you would
> want this which does the above but recursively.
>
> from collections import defaultdict
>
> class hash(defaultdict):
> def __init__(self):
> defaultdict.__init__(self, hash)
>
> D=hash()
>
> D[1][2][3][4]=5
> D[1][4][5]=6
>
> print D
>
> --
> Nick Craig-Wood <n...@craig-wood.com> --http://www.craig-wood.com/nick
Nick,
I thought I'd save your hash implementation away in my bag of tricks:

# File: autovivifying_dict.py

from collections import defaultdict
class hash(defaultdict):

""" Used like a dict except sub-dicts automagically created as
needed

>>> D=hash()
>>> D[1][2][3][4]=5
>>> D[1][4][5]=6

>>> D
hash({1: hash({2: hash({3: hash({4: 5})}), 4: hash({5: 6})})})
>>> hash({1: hash({2: hash({3: hash({4: 5})}), 4: hash({7: 8})})})
hash({1: hash({2: hash({3: hash({4: 5})}), 4: hash({7: 8})})})
>>>

"""
def __init__(self, *a, **b):
defaultdict.__init__(self, hash, *a, **b)
def __repr__(self):
return "hash(%s)" % (repr(dict(self)),)

def _test():
import doctest
doctest.testmod()

if __name__ == "__main__":
_test()