Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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 删除字符串中每个句子中特定符号后的单词 这是字符串,例如: “我有一个苹果。我想吃。但它太痛了。” 我想把它转换成这个: “我有一个苹果要吃,它太酸了”_Python_String_Indexing_Del - Fatal编程技术网

Python 删除字符串中每个句子中特定符号后的单词 这是字符串,例如: “我有一个苹果。我想吃。但它太痛了。” 我想把它转换成这个: “我有一个苹果要吃,它太酸了”

Python 删除字符串中每个句子中特定符号后的单词 这是字符串,例如: “我有一个苹果。我想吃。但它太痛了。” 我想把它转换成这个: “我有一个苹果要吃,它太酸了”,python,string,indexing,del,Python,String,Indexing,Del,下面是一种不使用正则表达式的方法,如您所述,使用del: def remove_after_sym(s, sym): # Find first word first = s.strip().split(' ')[0] # Split the string using the symbol l = [] s = s.strip().split(sym) # Split words by space in each sentence for

下面是一种不使用正则表达式的方法,如您所述,使用
del

def remove_after_sym(s, sym):
    # Find first word
    first = s.strip().split(' ')[0]

    # Split the string using the symbol
    l = []
    s = s.strip().split(sym)

    # Split words by space in each sentence
    for a in s:
        x = a.strip().split(' ')
        del x[0]
        l.append(x)

    # Join words in each sentence
    for i in range(len(l)):
        l[i] = ' '.join(l[i])

    # Combine sentences
    final = first + ' ' + ' '.join(l)
    final = final.strip() + '.'

    return final
这里,sym是一个
str
(单个字符)

此外,我还非常广泛地使用了“句子”一词,如您的示例中所示,
sym
是一个点。但这里的句子实际上是指被你想要的符号打断的部分字符串

下面是它的输出

In [1]: remove_after_sym(string, '.')
Out[1]: 'I have an apple want to eat it it is so sore.'

请指定您在标记中使用的编程语言为什么在“eat”之后的行中有2个“is”?据我所知,您希望删除“.”之后的单词,对吗?假设输出中的
它是
它是
并且
s
是您的字符串<代码>''.join([y代表y在[x代表x在s.replace('.','.$).split()]如果“$”不在y中。).replace(“.”,”)