Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #71216

Re: How to implement key of key in python?

Date 2014-05-10 03:30 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: How to implement key of key in python?
References <1ba8744e-943b-4c71-abd7-9dea12db8780@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.9840.1399689009.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 2014-05-10 02:22, eckhleung@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?
>
Python doesn't have Perl's autovivication feature. If you want the
value to be a dict then you need to create that dict first:

attr[ID] = {}
attr[ID]['adm3'] = row[2]

You could also have a look at the 'defaultdict' class in the
'collections' module.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to implement key of key in python? eckhleung@gmail.com - 2014-05-09 18:22 -0700
  Re: How to implement key of key in python? CHIN Dihedral <dihedral88888@gmail.com> - 2014-05-09 19:21 -0700
  Re: How to implement key of key in python? MRAB <python@mrabarnett.plus.com> - 2014-05-10 03:30 +0100
    Re: How to implement key of key in python? eckhleung@gmail.com - 2014-05-09 20:28 -0700
      Re: How to implement key of key in python? Andrea D'Amore <anddamNOALPASTICCIODICARNE+gruppi@brapi.net> - 2014-05-10 09:07 +0200
      Re: How to implement key of key in python? Peter Otten <__peter__@web.de> - 2014-05-10 10:21 +0200

csiph-web