Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #58906

Re: my favorite line of py code so far

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
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; 'args': 0.07; 'class,': 0.07; 'subject:code': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; 'iterable,': 0.16; 'itertools': 0.16; 'lambda': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'self.y': 0.16; 'starmap': 0.16; 'y):': 0.16; 'wrote:': 0.18; 'library': 0.18; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'case.': 0.24; 'tend': 0.24; "haven't": 0.24; 'header:X-Complaints-To:1': 0.27; 'needed.': 0.30; "i'm": 0.30; 'class': 0.32; 'probably': 0.32; 'skip:_ 10': 0.34; 'but': 0.35; 'there': 0.35; "i'll": 0.36; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'easy': 0.60; 'skip:y 20': 0.60; 'map': 0.64; 'obvious': 0.74; 'remember,': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: my favorite line of py code so far
Date Sat, 09 Nov 2013 08:12:25 +0100
Organization None
References <fbbabb45-f0e3-4e7f-968f-9822607c450a@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50849ee7.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.2279.1383981142.18130.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1383981142 news.xs4all.nl 15865 [2001:888:2000:d::a6]:44075
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:58906

Show key headers only | View raw


Peter Cacioppi wrote:

> my fav so far is this
> 
> _ = lambda c : lambda x : c(*x)
> 
> c can be any calleable and x any iterable, but I tend to use it with a
> class, and then map _(class) over the result of a zip.
> 
> It must be in the library somewhere, but I haven't found it. I'm never
> sure what to call it, so I just reroll it into _ as needed. It's pretty
> easy for me to remember, but I'll probably tattoo it on my chest just in
> case.

You mean

>>> class P:
...     def __init__(self, x, y):
...             self.x = x
...             self.y = y
...     def __repr__(self):
...             return "Point(x={0.x}, y={0.y})".format(self)
... 
>>> P(1, 2)
Point(x=1, y=2)
>>> _ = lambda c: lambda x: c(*x)
>>> list(map(_(P), zip([1,2,3], [6, 5, 4])))
[Point(x=1, y=6), Point(x=2, y=5), Point(x=3, y=4)]

? While the obvious approach would be

>>> [P(*args) for args in zip([1,2,3], [6, 5, 4])]
[Point(x=1, y=6), Point(x=2, y=5), Point(x=3, y=4)]

there is also itertools.starmap():

>>> from itertools import starmap
>>> list(starmap(P, zip([1,2,3], [6, 5, 4])))
[Point(x=1, y=6), Point(x=2, y=5), Point(x=3, y=4)]

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

my favorite line of py code so far Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-11-08 14:22 -0800
  Re: my favorite line of py code so far Chris Angelico <rosuav@gmail.com> - 2013-11-09 09:32 +1100
  Re: my favorite line of py code so far Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-11-08 14:43 -0800
  Re: my favorite line of py code so far Peter Otten <__peter__@web.de> - 2013-11-09 08:12 +0100
  Re: my favorite line of py code so far Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-11-09 01:23 -0800
    Re: my favorite line of py code so far Chris Angelico <rosuav@gmail.com> - 2013-11-09 20:28 +1100
    Re: my favorite line of py code so far Paul Rubin <no.email@nospam.invalid> - 2013-11-09 01:49 -0800
    Re: my favorite line of py code so far Peter Otten <__peter__@web.de> - 2013-11-09 12:49 +0100
    Re: my favorite line of py code so far Stefan Behnel <stefan_ml@behnel.de> - 2013-11-09 13:41 +0100
    Re: my favorite line of py code so far Chris Angelico <rosuav@gmail.com> - 2013-11-09 23:45 +1100
    Re: my favorite line of py code so far Peter Otten <__peter__@web.de> - 2013-11-09 14:49 +0100
  Re: my favorite line of py code so far Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-11-10 00:57 -0800
    Re: my favorite line of py code so far Chris Angelico <rosuav@gmail.com> - 2013-11-10 21:23 +1100
  Re: my favorite line of py code so far Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-11-10 00:59 -0800
  Re: my favorite line of py code so far Peter Cacioppi <peter.cacioppi@gmail.com> - 2013-11-10 16:00 -0800

csiph-web