问题描述:

so i'm just a noob in python and i was doing this exercise:

"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"."

I was able to do it here is my code :

def translate (var1):

vaw = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z']

var1 = list(var1)

for string in var1:

if string == string in vaw:

var1[var1.index(string)] = string + 'o' + string

print ''.join(var1)

I was wondering if this is correct or if there's another way to do it with less code?

网友答案:
def translate(s):
  consonants = 'bcdfghjklmnpqrstvwxz'
  return ''.join(l + 'o' + l if l in consonants else l for l in s)

print(translate("this is fun"))
网友答案:

regex is a good solution

>>> import re
>>> print re.sub(r"([bcdfghjklmnpqrstvwxyz])",r"\1o\1","this is fun")
tothohisos isos fofunon
网友答案:

First, you don't need to do this:

vaw = ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','z']

… to get a sequence of characters. A string is already a sequence of characters. (The same is true for var1, but there, your code is requiring a mutable sequence of characters, where you can replace any character with a longer string, so you do need a list.)

Also, your code doesn't actually work, because string == string in vaw is the same as True in vaw, which is always false. I think you meant if string in vaw. While we're at it, I wouldn't call a variable string, because that's the name of a built-in module.

And you can save a few keystrokes by not adding extra whitespace in places where standard Python style (PEP8) says not to. :)

So:

def translate(var1):
    vaw = 'bcdfghjklmnpqrstvwxz'
    var1 = list(var1)
    for s in var1:
        if s in vaw:
           var1[var1.index(s)] = s + 'o' + s
    print ''.join(var1)

Next, if you want the index of each element in var1, you don't want to throw it away, and then find it again with index. Besides being more code and slower, it's also going to give you the wrong answer for any element that appears more than once. So:

def translate(var1):
    vaw = 'bcdfghjklmnpqrstvwxz'
    var1 = list(var1)
    for i, s in enumerate(var1):
        if s in vaw:
           var1[i] = s + 'o' + s
    print ''.join(var1)

This is about as far as you can go if you want to mutate the var list in-place. You could change it to do var1[i+1:i+1] = 'o' + s, to insert the new elements after the existing one, but then you have to iterate over a copy of var1 (you can't change the shape of anything while iterating over it), and you have to keep track of how your indexes shifted, and so on.

It's usually a lot simpler to just build a new structure than to modify the old one in place. That's what list comprehensions, generator expressions, map, filter, etc. are there for. For each element s of the original list, you want s + 'o' + s if it's in vaw, just s otherwise, right? You can translate that right into Python:

def translate (var1):
    vaw = 'bcdfghjklmnpqrstvwxz'
    new_var1 = (s + 'o' + s if s in vaw else s for s in var1)
    return ''.join(new_var1)
相关阅读:
Top