Re: How is this evaluated

2013-07-05 Thread Chris Angelico
On Fri, Jul 5, 2013 at 11:41 AM, Steven D'Aprano
steve+comp.lang.pyt...@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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How is this evaluated

2013-07-05 Thread Steven D'Aprano
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.pyt...@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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How is this evaluated

2013-07-04 Thread Antoon Pardon

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
--
http://mail.python.org/mailman/listinfo/python-list


Re: How is this evaluated

2013-07-04 Thread newspost2012
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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How is this evaluated

2013-07-04 Thread Jussi Piitulainen
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)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How is this evaluated

2013-07-04 Thread Steven D'Aprano
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
-- 
http://mail.python.org/mailman/listinfo/python-list