在字符串中有5个或5个以上字母的单词中,尝试颠倒字母顺序.此代码不会崩溃,但也不会输出任何内容


def spin(s):
for word in s:
if len(word) >= 5:
w = ' '.join(w[::-1] for w in s.split())
return w
print(w)
s = "Twist and shout"
spin(s)

所需输出:"tsiwT和tuohs">

def spin(s):
return ' '.join(word[::-1] if len(word) >= 5 else word for word in s.split())
print(spin("Twist and shout"))
def reverse(x):
x_list = list(s.split(" "))
final_list = []
for word in x_list:
if len(word)>=5:
word = word[::-1]
final_list.append(word)
else:
final_list.append(word)
final_word = " ".join(final_list)
return final_word

占位符,而不是拆分操作。只是为了显示占位符的使用

string = "Twist and shout";
count = 0;
temp = 0;
array = [];
for element in range(0, len(string)): 
count = count + 1; 
if string[element] is " ":
array.append(string[temp:count-1]);
if len(array[-1]) > 4:
array[-1] = (array[-1][::-1]) 
temp = count;
array.append(string[temp:count]);
if len(array[-1]) > 4:
array[-1] = (array[-1][::-1]) 

相关内容

最新更新