Python脚本:添加";ing";对于文本中的任何单词,如果单词以“结束”;ing";加上「;“ly”;到它(例如:gging=ggingly)
这是我目前的代码:Python脚本:添加";ing";对于文本中的任何单词,如果单词以“结束”;ing";加上「;“ly”;到它(例如:gging=ggingly),python,string,list,Python,String,List,这是我目前的代码: lista = 'Text messaging, or texting, is the act of composing and sending electronic messages, typically consis' n = lista.split() m= '' def adding(n): for s in n: if s.endswith('ing'): s +='ly' else: s +='ing'
lista = 'Text messaging, or texting, is the act of composing and sending electronic messages, typically consis'
n = lista.split()
m= ''
def adding(n):
for s in n:
if s.endswith('ing'):
s +='ly'
else:
s +='ing'
return s
print(adding(n))
我应该在某个地方使用.join
,但我想不出来。
谢谢大家! 实际上,您需要遍历函数中的列表
n
。还要为输出创建一个新列表。以下是一个例子:
def adding(lst):
out = []
for word in lst:
if word.endswith("ing"):
out.append(word+"ly")
else:
out.append(word+"ing")
return out.join(" ")
print(adding(n()))
嗨,我不知道你说的是什么意思。这是我的版本,改进是忽略标点符号。还不知道如何。请查看上面我编辑的版本。