Search code examples
pythonletter

How to replace each letters in multiple words


I have an array like this :

input =  ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine" "thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"] 

i would like output = ["thirty", "forty", "sixty", "seventy", "eighty", "ninety"]

import re 
chaine = ["thirtheen", "fourteen", "fifteen"] 
print re.sub(r'[een]', 'y', ' '.join(chaine))  

I try a lot of method (replace .. ) but i am stuck :/


Solution

  • This will help you.

    chain = ["one", "two", "three", "four", "five", "six", "seven", "eight", "nine","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"] 
    
    
    new_chain = []
    
    for _ in chain:
        if _ == "fourteen":
            new_chain.append("forty")
        elif str.endswith(_,"een"):
            new_chain.append(_[:-3]+"y")
    print new_chain
    

    OUTPUT:

    ['thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']