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

James Stroud jstroud at mbi.ucla.edu
Thu Aug 16 23:37:57 EDT 2007


beginner wrote:
> On Aug 16, 9:32 pm, Carsten Haese <cars... at uniqsys.com> wrote:
> 
>>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 Haesehttp://informixdb.sourceforge.net
> 
> 
> 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).
> 

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

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

http://www.jamesstroud.com/



More information about the Python-list mailing list