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


Groups > comp.lang.python > #49891

Re: How is this evaluated

Date 2013-07-04 20:05 +0200
From Antoon Pardon <antoon.pardon@rece.vub.ac.be>
Subject Re: How is this evaluated
References <81f466df-5203-48a7-97a8-643b70d9dd16@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.4245.1372961165.3114.python-list@python.org> (permalink)

Show all headers | View raw


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

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


Thread

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

csiph-web