刷到現在的感受是:不會的命令一定及時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(' '))
基本上一個意思,真的好佩服會寫單行代碼的。。。。。