How to implement key of key in python?

CHIN Dihedral dihedral88888 at gmail.com
Fri May 9 22:21:34 EDT 2014


On Saturday, May 10, 2014 9:22:43 AM UTC+8, eckh... at gmail.com wrote:
> I'm migrating from Perl to Python and unable to identify the equivalent of key of key concept. The following codes run well,
> 
> 
> 
> import csv                                                                       
> 
>                                 
> 
> attr = {}                                                                        
> 
>                                                                                  
> 
> with open('test.txt','rb') as tsvin:                                           
> 
>     tsvin = csv.reader(tsvin, delimiter='\t')                                    
> 
>                                                                                  
> 
>     for row in tsvin:                                                            
> 
>         ID = row[1]                                                             
> 
> 
> 
> 
> 
> until:
> 
>         attr[ID]['adm3'] = row[2]  
> 
> 
> 
> I then try:
> 
>         attr[ID].adm3 = row[2]
> 
> 
> 
> still doesn't work. Some posts suggest using module dict but some do not. I'm a bit confused now. Any suggestions?

Please check your attr as an empty dictionary or so-called a hash in perl.

The syntax of adding a (K,V) pair 
is different between python and perl.



More information about the Python-list mailing list