我编写了一个函数,该函数以消息(字符串)作为输入,并将每个单词的第一个字母替换为前一个单词的第一个字母(对于第一个单词,我取了最后一个单词的第一个字母):
def changeFirst(message):
msg_list=list(message)
j=0
pre=msg_list[j]
for i in range(len(msg_list)):
if msg_list[i]==' ':
nextpre=msg_list[i+1]
msg_list[i+1]=pre
pre=nextpre
msg_list[0]=pre
msg_list=''.join(msg_list)
return msg_list
changeFirst("now you are in love with me")
mow nou yre an iove lith we
我想编写一个功能UnchangeFirst()
来反转此功能,例如,它应该像这样工作:
UnchangeFirst("mow nou yre an iove lith we")
now you are in love with me
如何取消此功能?
这里是!
def changedAgain(message2):
msg_list = list(message2)
j = 0
first=msg_list[0]
for i in range(len(msg_list)):
if msg_list[i]==' ':
msg_list[j]=msg_list[i+1]
msg_list[i+1]=msg_list[j]
j=i+1
msg_list[j]=first
return(''.join(msg_list))
message2="mow nou yre an iove lith we"
print(changedAgain(message2))
>>> "now you are in love with me"
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句