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!newsfeed.xs4all.nl!newsfeed5.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'linear': 0.07; 'read- only': 0.07; 'subject:file': 0.07; 'url:pycon': 0.07; 'appropriate.': 0.09; 'brackets': 0.09; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'tuple': 0.09; 'whichever': 0.09; "wouldn't": 0.11; '(2,': 0.16; "(i'm": 0.16; '3):': 0.16; 'cleanly': 0.16; 'generator.': 0.16; 'iterating': 0.16; 'iteration.': 0.16; 'message- id:@dough.gmane.org': 0.16; 'readable': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'semicolon': 0.16; 'set-like': 0.16; 'wrote:': 0.17; 'version.': 0.17; 'jan': 0.18; 'tim': 0.18; '>>>': 0.18; 'appropriate': 0.20; '3.x': 0.22; 'keys': 0.22; 'work.': 0.23; "python's": 0.23; 'specifically': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'values': 0.26; 'entries': 0.27; "doesn't": 0.28; 'header:X-Complaints-To:1': 0.28; 'this?': 0.28; '>>>>': 0.29; 'chase': 0.29; 'overhead': 0.29; 'point': 0.31; 'url:python': 0.32; 'file': 0.32; 'print': 0.32; 'skip:\xd0 10': 0.33; 'to:addr:python-list': 0.33; "can't": 0.34; 'list': 0.35; 'faster': 0.35; 'pm,': 0.35; 'something': 0.35; 'received:org': 0.36; 'item': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'link': 0.60; '8bit%:84': 0.62; 'more': 0.63; 'making': 0.64; 'touch': 0.69; 'received:fios.verizon.net': 0.84; 'url:2007': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: save dictionary to a file without brackets. Date: Thu, 09 Aug 2012 17:46:29 -0400 References: <930ab3d8-4ab9-446d-9970-ee811eb70a44@googlegroups.com> <50241F14.2060209@tim.thechases.com> <36EA3847-6713-4C12-B47B-9B5E10325F00@gmail.com> <502429C3.5000600@tim.thechases.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20120713 Thunderbird/14.0 In-Reply-To: <502429C3.5000600@tim.thechases.com> 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: 52 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1344548828 news.xs4all.nl 6909 [2001:888:2000:d::a6]:44852 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:26818 On 8/9/2012 5:21 PM, Tim Chase wrote: > On 08/09/12 15:41, Roman Vashkevich wrote: >> 10.08.2012, =D0=B2 0:35, Tim Chase =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0= =D0=BB(=D0=B0): >>> 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 semico= lon 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) >> >> 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 > > That link doesn't actually discuss dict.{iter}items() > > Both are O(N) because you have to touch each item in the dict--you > can't iterate over N entries in less than O(N) time. For small > data-sets, building the list and then iterating over it may be > faster faster; for larger data-sets, the cost of building the list > overshadows the (minor) overhead of a generator. Either way, the > iterate-and-fetch-the-associated-value of .items() & .iteritems() > can (should?) be optimized in Python's internals to the point I > wouldn't think twice about using the more readable version. In 3.x, .keys, .values, and .items are set-like read-only views=20 specifically designed for iteration. So in 3.x they are THE way to do so = for whichever alternative is appropriate. Iterating by keys and then=20 looking up values instead of yielding the values at the same time is=20 extra work. --=20 Terry Jan Reedy