Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #49889 > unrolled thread
| Started by | Arturo B <a7xrturodev@gmail.com> |
|---|---|
| First post | 2013-07-04 10:20 -0700 |
| Last post | 2013-07-05 08:38 +0000 |
| Articles | 7 — 6 participants |
Back to article view | Back to comp.lang.python
How is this evaluated Arturo B <a7xrturodev@gmail.com> - 2013-07-04 10:20 -0700
Re: How is this evaluated Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2013-07-04 20:05 +0200
Re: How is this evaluated newspost2012@gmx.de - 2013-07-04 11:07 -0700
Re: How is this evaluated Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-07-04 21:22 +0300
Re: How is this evaluated Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-05 01:41 +0000
Re: How is this evaluated Chris Angelico <rosuav@gmail.com> - 2013-07-05 17:05 +1000
Re: How is this evaluated Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-05 08:38 +0000
| From | Arturo B <a7xrturodev@gmail.com> |
|---|---|
| Date | 2013-07-04 10:20 -0700 |
| Subject | How is this evaluated |
| Message-ID | <81f466df-5203-48a7-97a8-643b70d9dd16@googlegroups.com> |
I'm making this exercise: (Python 3.3)
Write a function translate() that will translate a text into "rövarspråket" (Swedish for "robber's language"). That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun") should return the string "tothohisos isos fofunon".
So I tried to solved it, but I couldn't, so I found many answers, but I selected this:
def translate(s):
consonants = 'bcdfghjklmnpqrstvwxz'
return ''.join(l + 'o' + l if l in consonants else l for l in s)
print(translate('hello code solver'))
OUTPUT:
'hohelollolo cocodode sosololvoveror'
______________________________________________________________
So I want to question:
How is the
if 'h' in consonants else 'h' for 'h' in s
part evaluated? (step by step please :P )
''.join('h' + 'o' + 'h' if 'h' in consonants else 'h' for 'h' in s)
Thank you guys
[toc] | [next] | [standalone]
| From | Antoon Pardon <antoon.pardon@rece.vub.ac.be> |
|---|---|
| Date | 2013-07-04 20:05 +0200 |
| Message-ID | <mailman.4245.1372961165.3114.python-list@python.org> |
| In reply to | #49889 |
Op 04-07-13 19:20, Arturo B schreef:
> I'm making this exercise: (Python 3.3)
>
> Write a function translate() that will translate a text into "rövarspråket" (Swedish for "robber's language"). That is, double every consonant and place an occurrence of "o" in between. For example, translate("this is fun") should return the string "tothohisos isos fofunon".
>
> So I tried to solved it, but I couldn't, so I found many answers, but I selected this:
>
> def translate(s):
> consonants = 'bcdfghjklmnpqrstvwxz'
> return ''.join(l + 'o' + l if l in consonants else l for l in s)
>
> print(translate('hello code solver'))
>
>
> OUTPUT:
> 'hohelollolo cocodode sosololvoveror'
>
> ______________________________________________________________
> So I want to question:
> How is the
>
> if 'h' in consonants else 'h' for 'h' in s
>
> part evaluated? (step by step please :P )
>
> ''.join('h' + 'o' + 'h' if 'h' in consonants else 'h' for 'h' in s)
>
> Thank you guys
This doesn't make much sence because you substituted a varible for
a character literal, so I'll go with the original.
l + 'o' + l if l in consonants else l for l in s
Evaluate this expression:
l + 'o' + l if l in consonants else l
for each l in s (so for each letter that in s).
l + 'o' + l if l in consonants else l
if l is part of the consonants produce l + 'o' + l
otherwise just produce l.
Hope this helps.
--
Antoon Pardon
[toc] | [prev] | [next] | [standalone]
| From | newspost2012@gmx.de |
|---|---|
| Date | 2013-07-04 11:07 -0700 |
| Message-ID | <882c9c1a-f2aa-4af5-b473-ea0c0d61e71e@googlegroups.com> |
| In reply to | #49889 |
Am Donnerstag, 4. Juli 2013 19:20:43 UTC+2 schrieb Arturo B: > ... > So I want to question: > How is the > > if 'h' in consonants else 'h' for 'h' in s > > part evaluated? (step by step please :P ) Although new to python I think I can solve this (if no one contradicts, I can guess that I understood it :-) ): Take every letter from the string, one after the other, (for l in s). If you can find the letter in the string constants (if l in constants) then take it, append an "o" and then the letter again and return it (l + 'o' + l) if not, return the letter as it is (else l). Or in applicable order: take 'h' append 'o' append 'h' and return this, if you can find 'h' in constants, if not return just 'h' (and do this for every letter in string s) hth, Martin
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2013-07-04 21:22 +0300 |
| Message-ID | <qot61wqmbhe.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #49889 |
Arturo B writes:
> I'm making this exercise: (Python 3.3)
>
> Write a function translate() that will translate a text into
> "rövarspråket" (Swedish for "robber's language"). That is, double
> every consonant and place an occurrence of "o" in between. For
> example, translate("this is fun") should return the string
> "tothohisos isos fofunon".
>
> So I tried to solved it, but I couldn't, so I found many answers,
> but I selected this:
>
> def translate(s):
> consonants = 'bcdfghjklmnpqrstvwxz'
> return ''.join(l + 'o' + l if l in consonants else l for l in s)
>
> print(translate('hello code solver'))
>
>
> OUTPUT:
> 'hohelollolo cocodode sosololvoveror'
>
> ______________________________________________________________
> So I want to question:
> How is the
>
> if 'h' in consonants else 'h' for 'h' in s
>
> part evaluated? (step by step please :P )
That's nonsense in two different ways. The actual expression in the
solution you found makes sense. It's a generator expression of this
form:
expression for l in s
And the expression in it is this conditional expression:
l + 'o' + l if l in consonants else l
The value of this conditional expression is (l + 'o' + l) if l is in
consontants, for example 'tot' if l == 't'; else it is just l, for
example 'i' if l == 'i'.
Fully parenthesised the whole expression is this:
((l + 'o' + l) if (l in consonants) else l) for l in s
The value of this expression yields the values like 'tot' and 'hoh'
and 'i' for each letter l in s in turn.
> ''.join('h' + 'o' + 'h' if 'h' in consonants else 'h' for 'h' in s)
This is still nonsense. To make sense, replace each 'h' with h.
Then ''.join can eat up the values of a generator expression to
produce the string like 'tothohi...'.
You can experiment with the components of this complicated expression:
>>> list(x for x in 'plaintext')
>>> 'x' if True else 'y'
>>> 'x' if False else 'y'
>>> list((x + 'o' if x not in 'aeiouy' else x) for x in 'plaintext')
>>> for x in 'plaintext': print(x + 'o' if x not in 'aeiouy' else x)
I suppose you understand this:
>>> ''.join(('tot', 'hoh', 'i', 'sos'))
Note that a similar-looking expression in brackets [] is a list
comprehension; in braces {} it is a set comprehension, or a dictionary
comprehension if the value expression is a key : value pair, with the
colon. And this is not all: there's nesting and filtering, too. The
different uses of the keywords 'for', 'in', 'if', 'else' are a bit
subtle but one sort of learns to see the whole expression.
I tend to use line breaks and parentheses in such expressions:
''.join(c + 'o' + c if c in consonants else c
for c in message)
''.join((c + 'o' + c
if c in consonants
else c)
for c in message)
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-07-05 01:41 +0000 |
| Message-ID | <51d62451$0$29999$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #49889 |
On Thu, 04 Jul 2013 10:20:43 -0700, Arturo B wrote:
> I'm making this exercise: (Python 3.3)
>
> Write a function translate() that will translate a text into
> "rövarspråket" (Swedish for "robber's language"). That is, double every
> consonant and place an occurrence of "o" in between. For example,
> translate("this is fun") should return the string "tothohisos isos
> fofunon".
>
> So I tried to solved it, but I couldn't, so I found many answers, but I
> selected this:
>
> def translate(s):
> consonants = 'bcdfghjklmnpqrstvwxz'
> return ''.join(l + 'o' + l if l in consonants else l for l in s)
The part inside the join(...) is called a generator expression.
I do not recommend generator expressions (or list comprehensions) for
beginners, because they can be confusing until you understand how they
work. As a beginner, if you have a choice between a one-line piece of
code that makes no sense to you, and a five-line piece of code that you
understand, you should choose the one that you understand.
> So I want to question:
> How is the
>
> if 'h' in consonants else 'h' for 'h' in s
>
> part evaluated? (step by step please :P )
That is the wrong question, because you have split the code in the wrong
places. Your generator expression looks like this:
l + 'o' + l if l in consonants else l for l in s
This is a "generator expression" of the form:
(x for l in s)
where x will be defined below. This part is easy enough to understand:
Python will iterate over the string s, extracting one letter at a time l,
and then evaluate the expression "x" below.
What is the expression "x"? It is this part:
(l + 'o' + l if l in consonants else l)
which forms the "ternary operator" if-clause:
value-if-true if condition-being-tested else value-if-false
If you know C, that's like:
?(condition-being-tested, value-if-true, value-if-false)
The condition being tested is, "l in consonants". The value if true is "l
+ 'o' + l". And the value if false is just l.
So putting this all together, we can convert the generator expression
version to this longer, but more readable, version:
def translate(s):
consonants = 'bcdfghjklmnpqrstvwxz'
collector = []
for l in s:
if l in consonants:
collector.append(l + 'o' + l)
else:
collector.append(l)
return ''.join(collector)
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-07-05 17:05 +1000 |
| Message-ID | <mailman.4269.1373007952.3114.python-list@python.org> |
| In reply to | #49918 |
On Fri, Jul 5, 2013 at 11:41 AM, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > If you know C, that's like: > > ?(condition-being-tested, value-if-true, value-if-false) Or to be precise: condition-being-tested ? value-if-true : value-if-false ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2013-07-05 08:38 +0000 |
| Message-ID | <51d685e8$0$29999$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #49931 |
On Fri, 05 Jul 2013 17:05:49 +1000, Chris Angelico wrote: > On Fri, Jul 5, 2013 at 11:41 AM, Steven D'Aprano > <steve+comp.lang.python@pearwood.info> wrote: >> If you know C, that's like: >> >> ?(condition-being-tested, value-if-true, value-if-false) > > Or to be precise: > > condition-being-tested ? value-if-true : value-if-false Oops. Sorry about that. I thought it looked wrong even as I was typing it. -- Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web