python algorithm: Reverse words

The feeling of brushing up to now is: the command that does not know must be Googled in time

hahahahaha

Write a reverseWords function that accepts a string a parameter, and reverses each word in the string. Any spaces in the string should be retained.

Example:
reverse_words("This is an example!") # returns  "sihT si na !elpmaxe"

reverse_words("double  spaces") # returns  "elbuod  secaps"



My solution: [I used an empty table as a container, and also used slice-flip and join ]

def reverse_words(str):
    l = str.split ('')
    ll=[]
    for i in l:
        j=i[::-1]
        ll.append(j)
    a=' '.join(ll)
    return a



Okami solution:

def reverse_words(str):
    return ' '.join(s[::-1] for s in str.split(' '))
Basically a meaning, I really admire the ability to write a single line of code. . . . .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325455908&siteId=291194637