刷到现在的感受是:不会的命令一定及时Google
哈哈哈哈哈
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"
我的解法:【用了空表做容器,还用了切片-翻转和join】
def reverse_words(str): l=str.split(' ') ll=[] for i in l: j=i[::-1] ll.append(j) a=' '.join(ll) return a
大神解法:
def reverse_words(str): return ' '.join(s[::-1] for s in str.split(' '))基本上一个意思,真的好佩服会写单行代码的。。。。。