Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!news2.euro.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'linear': 0.07; 'subject:file': 0.07; 'url:pycon': 0.07; 'brackets': 0.09; 'dict': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tuple': 0.09; '(2,': 0.16; "(i'm": 0.16; '-tkc': 0.16; '3):': 0.16; 'cleanly': 0.16; 'message-id:@dough.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'semicolon': 0.16; 'wrote:': 0.17; 'tim': 0.18; '>>>': 0.18; 'appropriate': 0.20; 'tonight': 0.22; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'entries': 0.27; 'header:X-Complaints-To:1': 0.28; 'this?': 0.28; '>>>>': 0.29; 'chase': 0.29; 'starts': 0.29; "i'm": 0.29; "we're": 0.30; 'url:python': 0.32; 'file': 0.32; 'from:addr:yahoo.co.uk': 0.32; 'print': 0.32; 'to:addr:python- list': 0.33; 'list': 0.35; 'something': 0.35; 'there': 0.35; 'received:org': 0.36; "i'll": 0.36; 'subject:: ': 0.38; 'mark': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'ever': 0.63; 'more': 0.63; 'making': 0.64; 'blast': 0.84; 'url:2007': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: save dictionary to a file without brackets. Date: Thu, 09 Aug 2012 22:17:50 +0100 References: <930ab3d8-4ab9-446d-9970-ee811eb70a44@googlegroups.com> <50241F14.2060209@tim.thechases.com> <36EA3847-6713-4C12-B47B-9B5E10325F00@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=KOI8-R; format=flowed Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: host-92-24-125-189.ppp.as43234.net User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:14.0) Gecko/20120713 Thunderbird/14.0 In-Reply-To: <36EA3847-6713-4C12-B47B-9B5E10325F00@gmail.com> X-Antivirus: avast! (VPS 120809-0, 09/08/2012), Outbound message X-Antivirus-Status: Clean X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1344547004 news.xs4all.nl 6847 [2001:888:2000:d::a6]:55744 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26813 On 09/08/2012 21:41, Roman Vashkevich wrote: > dict.items() is a list - linear access time whereas with 'for key in dict:' access time is constant: http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#use-in-where-possible-1 > > 10.08.2012, Χ 0:35, Tim Chase ΞΑΠΙΣΑΜ(Α): > >> On 08/09/12 15:22, Roman Vashkevich wrote: >>>> {(4, 5): 1, (5, 4): 1, (4, 4): 2, (2, 3): 1, (4, 3): 2} >>>> and i want to print to a file without the brackets comas and semicolon in order to obtain something like this? >>>> 4 5 1 >>>> 5 4 1 >>>> 4 4 2 >>>> 2 3 1 >>>> 4 3 2 >>> >>> for key in dict: >>> print key[0], key[1], dict[key] >> >> This might read more cleanly with tuple unpacking: >> >> for (edge1, edge2), cost in d.iteritems(): # or .items() >> print edge1, edge2, cost >> >> (I'm making the assumption that this is a edge/cost graph...use >> appropriate names according to what they actually mean) >> >> -tkc >> >> >> > I'm impressed, the OP gives a dict with five entries and already we're optimising, a cunning plan if ever there was one. Hum, I think I'll start on the blast proof ferro-concrete bunker tonight just in case WWIII starts tomorrow. -- Cheers. Mark Lawrence.