Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #17536 > unrolled thread
| Started by | Gnarlodious <gnarlodious@gmail.com> |
|---|---|
| First post | 2011-12-19 18:04 -0800 |
| Last post | 2011-12-19 19:40 -0800 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
Transform two tuples item by item Gnarlodious <gnarlodious@gmail.com> - 2011-12-19 18:04 -0800
Re: Transform two tuples item by item Tim Chase <python.list@tim.thechases.com> - 2011-12-19 20:42 -0600
Re: Transform two tuples item by item Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-20 03:40 +0000
Re: Transform two tuples item by item Gnarlodious <gnarlodious@gmail.com> - 2011-12-19 19:40 -0800
| From | Gnarlodious <gnarlodious@gmail.com> |
|---|---|
| Date | 2011-12-19 18:04 -0800 |
| Subject | Transform two tuples item by item |
| Message-ID | <21814930.66.1324346655123.JavaMail.geo-discussion-forums@prmw6> |
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], "<span class='H'>{}</span>".format(tup[1]), bool(tup[2]), bool(tup[3]), tup[4], bool(tup[5]), escape(tup[6])]
-> [35, "<span class='H'>34</span>", False, True, 31, False, '&#26082;&#28639;']
But I want to loop rather than subscripting.
-- Gnarlie
[toc] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2011-12-19 20:42 -0600 |
| Message-ID | <mailman.3840.1324348945.27778.python-list@python.org> |
| In reply to | #17536 |
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], "<span> class='H'>{}</span>".format(tup[1]), bool(tup[2]),
> bool(tup[3]), tup[4], bool(tup[5]), escape(tup[6])]
>
> -> [35, "<span class='H'>34</span>", 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, "<span class='H'>{0}</span>".format, bool, bool,
nop, bool, escape]
>>> [f(x) for f, x in zip(ops,tup)]
[35, "<span class='H'>34</span>", 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
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-12-20 03:40 +0000 |
| Message-ID | <4ef00394$0$11091$c3e8da3@news.astraweb.com> |
| In reply to | #17536 |
On Mon, 19 Dec 2011 18:04:15 -0800, 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:
Create a list of functions:
ops = [lambda obj: obj,
"<span class='H'>{}</span>".format,
bool,
bool,
lambda obj: obj,
bool,
escape,
]
then use zip to pair them up with their arguments:
results = [f(x) for f,x in zip(ops, tup)]
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Gnarlodious <gnarlodious@gmail.com> |
|---|---|
| Date | 2011-12-19 19:40 -0800 |
| Message-ID | <10253307.963.1324352410950.JavaMail.geo-discussion-forums@pruu23> |
| In reply to | #17536 |
Wow, that is so elegant. Python is awesome. -- Gnarlie
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web