Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!81.171.88.16.MISMATCH!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone03.am1.xlned.com!bcyclone03.am1.xlned.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'assign': 0.07; 'attribute': 0.07; '__name__': 0.09; 'essential.': 0.09; 'true)': 0.09; "'__main__':": 0.16; 'from:addr:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 0.16; 'simpson': 0.16; 'skip:> 20': 0.16; 'subject:???': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'print': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'refers': 0.24; 'cheers,': 0.24; 'appreciated': 0.26; 'second': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'statement': 0.30; 'subject:list': 0.30; 'gives': 0.31; 'code': 0.31; 'file': 0.32; 'run': 0.32; '(most': 0.33; 'call.': 0.33; 'skip:# 10': 0.33; 'skip:s 30': 0.35; 'something': 0.35; 'received:com.au': 0.36; 'skip:> 10': 0.36; 'charset:us-ascii': 0.36; "i'll": 0.36; 'list': 0.37; 'list.': 0.37; 'thank': 0.38; 'received:211': 0.38; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'first': 0.61; 'content-disposition:inline': 0.62; 'complete': 0.62; 'email addr:gmail.com': 0.63; 'name': 0.63; 'reverse': 0.68; 'dict.': 0.84 Date: Fri, 5 Dec 2014 18:19:45 +1100 From: Cameron Simpson To: python-list@python.org Subject: Re: dict turn to list unexpected at runtime??? MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) References: X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.1 cv=BdjhjNd2 c=1 sm=1 tr=0 a=YuQlxtEQCowy2cfE5kc7TA==:117 a=YuQlxtEQCowy2cfE5kc7TA==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=J0QyKEt1u0cA:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=A92cGCtB03wA:10 a=pGLkceISAAAA:8 a=8rufUQI8ov7iGrEbcfoA:9 a=CjuIK1q_8ugA:10 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: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417763998 news.xs4all.nl 2971 [2001:888:2000:d::a6]:57067 X-Complaints-To: abuse@xs4all.nl X-Received-Body-CRC: 2821498685 X-Received-Bytes: 4911 Xref: csiph.com comp.lang.python:81982 On 05Dec2014 15:01, telnetgmike@gmail.com wrote: >Why the following code gives me errors??? And why the print statement run 2 times? I'll be appreciated your helps, thanks, >addrnum_dict = {'a':1,'b':2} >def orderaddrtimes(): > global addrnum_dict > print type(addrnum_dict) > > addrnum_dict = sorted(addrnum_dict.iteritems(), key=lambda d:d[1], reverse = True) Because of the line above. The return value of "sorted()" is a list. You assign that list to "addrnum_dict". That name now refers to the list from "sorted()". Because you call the orderaddrtimes() function twice, the second time you call it "addrnum_dict" is the list from the first call. Not a dict. You would be best to not assign the result of "sorte()" to "addrnum_dict". Assign it to something else. > #addrnum_dict = OrderedDict(sorted(addrnum_dict.items(), key=lambda t: t[0])) >if __name__ == '__main__': > kinds = ["a","b"] > for tmp_kind in kinds: > orderaddrtimes() > >###################### >errors: >python aaa.py > > >Traceback (most recent call last): > File "aaa.py", line 16, in > orderaddrtimes() > File "aaa.py", line 11, in orderaddrtimes > addrnum_dict = sorted(addrnum_dict.iteritems(), key=lambda d:d[1], reverse = True) >AttributeError: 'list' object has no attribute 'iteritems' Thank you for including the complete error output. This is very helpful, and often essential. Cheers, Cameron Simpson Who are all you people and why are you in my computer? - Kibo