Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python脚本:添加";ing";对于文本中的任何单词,如果单词以“结束”;ing";加上「;“ly”;到它(例如:gging=ggingly)_Python_String_List - Fatal编程技术网

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()))

嗨,我不知道你说的是什么意思。这是我的版本,改进是忽略标点符号。还不知道如何。请查看上面我编辑的版本。