Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'feature.': 0.09; 'try:': 0.09; 'subject:How': 0.10; 'python': 0.11; 'suggest': 0.14; 'attr': 0.16; 'csv': 0.16; 'dict': 0.16; 'first:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subject:key': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'module': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'equivalent': 0.26; 'posts': 0.26; 'header:In-Reply-To:1': 0.27; "doesn't": 0.30; "i'm": 0.30; 'work.': 0.31; "skip:' 10": 0.31; 'perl': 0.31; 'class': 0.32; 'run': 0.32; 'not.': 0.33; 'could': 0.34; 'received:84': 0.35; 'but': 0.35; 'module.': 0.36; 'subject:?': 0.36; 'skip:o 20': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'unable': 0.39; 'identify': 0.61; 'email addr:gmail.com': 0.63; 'concept.': 0.84; 'migrating': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=MtHc6gqe c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=1oDRhzKAKukA:10 a=OivJZsAZSu8A:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=pGLkceISAAAA:8 a=_G5MnYnfQtKvVffuZBQA:9 a=QEXdDO2ut3YA:10 a=MSl-tDqOz04A:10 X-AUTH: mrabarnett:2500 Date: Sat, 10 May 2014 03:30:06 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: How to implement key of key in python? References: <1ba8744e-943b-4c71-abd7-9dea12db8780@googlegroups.com> In-Reply-To: <1ba8744e-943b-4c71-abd7-9dea12db8780@googlegroups.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1399689009 news.xs4all.nl 2863 [2001:888:2000:d::a6]:42774 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:71216 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.