Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'escape': 0.04; 'subject:two': 0.04; 'false,': 0.09; 'tuple': 0.09; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'lambda': 0.16; 'message- id:@tim.thechases.com': 0.16; 'operations?': 0.16; 'ops': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'transforming': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'header:In- Reply-To:1': 0.22; 'cc:2**0': 0.24; 'function': 0.27; 'import': 0.27; 'cc:addr:gmail.com': 0.28; 'true,': 0.29; 'least': 0.30; "i've": 0.31; 'values': 0.32; 'list': 0.32; 'header:User-Agent:1': 0.33; 'rather': 0.33; 'to:addr:python-list': 0.34; 'it.': 0.34; 'loop': 0.34; 'calling': 0.34; 'puts': 0.34; 'something': 0.35; 'skip:" 10': 0.37; 'but': 0.37; 'reference': 0.37; 'list,': 0.37; "it's": 0.40; 'to:addr:python.org': 0.40; 'your': 0.61; 'results': 0.63; '31,': 0.64; '#1:': 0.84; '#2:': 0.84 Date: Mon, 19 Dec 2011 20:42:13 -0600 From: Tim Chase User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111120 Icedove/3.1.16 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Transform two tuples item by item References: <21814930.66.1324346655123.JavaMail.geo-discussion-forums@prmw6> In-Reply-To: <21814930.66.1324346655123.JavaMail.geo-discussion-forums@prmw6> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Source: X-Source-Args: X-Source-Dir: Cc: Gnarlodious 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: 37 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1324348945 news.xs4all.nl 6863 [2001:888:2000:d::a6]:49962 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:17537 On 12/19/11 20:04, Gnarlodious wrote: > What is the best way to operate on a tuple of values > transforming them against a tuple of operations? Result can be > a list or tuple: > > tup=(35, '34', 0, 1, 31, 0, '既濟') > > from cgi import escape > [tup[0], " class='H'>{}".format(tup[1]), bool(tup[2]), > bool(tup[3]), tup[4], bool(tup[5]), escape(tup[6])] > > -> [35, "34", False, True, 31, False, > '&#26082;&#28639;'] > > But I want to loop rather than subscripting. Well, you can do something like >>> from cgi import escape >>> nop = lambda x: x >>> tup = (35, '34', 0, 1, 31, 0, '既濟') >>> ops = [nop, "{0}".format, bool, bool, nop, bool, escape] >>> [f(x) for f, x in zip(ops,tup)] [35, "34", False, True, 31, False, '&#26082;&#28639;'] Note #1: I had to change your format from "{}" to "{0}", at least in 2.6 I've got here) Note #2: it's spelled ".format" not ".format()" which puts the function reference in the "ops" list, not the results of calling it. -tkc