Python 如何为任意字符串实现rstrip?

Python 如何为任意字符串实现rstrip?,python,Python,假设我有一串字符串,可以以菠萝、火腿或葡萄柚结尾。生成与原始字符串完全相同的“剥离字符串”列表的最佳方法是什么?除了如果字符串以菠萝结尾,菠萝将从末尾修剪,如果字符串以火腿结尾,火腿将被修剪之外 例如,假设我的输入是 ["I really like pineapple", "I don't like ham or grapefruit", "Today I ate a lot of ham", "but I also ate a lot of grapefruit"] 那么输出应该是 ["I

假设我有一串字符串,可以以
菠萝
火腿
葡萄柚
结尾。生成与原始字符串完全相同的“剥离字符串”列表的最佳方法是什么?除了如果字符串以
菠萝
结尾,菠萝将从末尾修剪,如果字符串以
火腿
结尾,火腿将被修剪之外

例如,假设我的输入是

["I really like pineapple",
"I don't like ham or grapefruit",
"Today I ate a lot of ham",
"but I also ate a lot of grapefruit"]
那么输出应该是

["I really like",
"I don't like ham or grapefruit",
"Today I ate a lot of",
"but I also ate a lot of grapefruit"]

似乎是
re
的工作。只需在表达式的末尾添加一个线端点锚点,使其仅在结尾处为子锚点

import re

stuff = ["I really like pineapple", "I don't like ham or grapefruit", "Today I ate a lot of ham", "but I also ate a lot of grapefruit"]

[re.sub(r'(pineapple|ham)$','',s).strip() for s in stuff]
Out[7]: 
['I really like',
 "I don't like ham or grapefruit",
 'Today I ate a lot of',
 'but I also ate a lot of grapefruit']
(我添加了一个额外的
strip()
来清除任何剩余的空白)

要使用任意字符串列表执行此操作,它将如下所示:

re.sub(r'({})$'.format('|'.join(arbitrary_list_of_strings)),'',s)...

似乎是
re
的工作。只需在表达式的末尾添加一个线端点锚点,使其仅在结尾处为子锚点

import re

stuff = ["I really like pineapple", "I don't like ham or grapefruit", "Today I ate a lot of ham", "but I also ate a lot of grapefruit"]

[re.sub(r'(pineapple|ham)$','',s).strip() for s in stuff]
Out[7]: 
['I really like',
 "I don't like ham or grapefruit",
 'Today I ate a lot of',
 'but I also ate a lot of grapefruit']
(我添加了一个额外的
strip()
来清除任何剩余的空白)

要使用任意字符串列表执行此操作,它将如下所示:

re.sub(r'({})$'.format('|'.join(arbitrary_list_of_strings)),'',s)...

似乎是
re
的工作。只需在表达式的末尾添加一个线端点锚点,使其仅在结尾处为子锚点

import re

stuff = ["I really like pineapple", "I don't like ham or grapefruit", "Today I ate a lot of ham", "but I also ate a lot of grapefruit"]

[re.sub(r'(pineapple|ham)$','',s).strip() for s in stuff]
Out[7]: 
['I really like',
 "I don't like ham or grapefruit",
 'Today I ate a lot of',
 'but I also ate a lot of grapefruit']
(我添加了一个额外的
strip()
来清除任何剩余的空白)

要使用任意字符串列表执行此操作,它将如下所示:

re.sub(r'({})$'.format('|'.join(arbitrary_list_of_strings)),'',s)...

似乎是
re
的工作。只需在表达式的末尾添加一个线端点锚点,使其仅在结尾处为子锚点

import re

stuff = ["I really like pineapple", "I don't like ham or grapefruit", "Today I ate a lot of ham", "but I also ate a lot of grapefruit"]

[re.sub(r'(pineapple|ham)$','',s).strip() for s in stuff]
Out[7]: 
['I really like',
 "I don't like ham or grapefruit",
 'Today I ate a lot of',
 'but I also ate a lot of grapefruit']
(我添加了一个额外的
strip()
来清除任何剩余的空白)

要使用任意字符串列表执行此操作,它将如下所示:

re.sub(r'({})$'.format('|'.join(arbitrary_list_of_strings)),'',s)...
如果要清除多余的空白,可以执行以下操作:

In [7]: L = ["I really like pineapple",
"I don't like ham or grapefruit",
"Today I ate a lot of ham",
"but I also ate a lot of grapefruit"]

In [8]: for i in range(len(L)):
    while any(L[i].endswith(suffix) for suffix in suffixes):
        for suffix in suffixes:
            if L[i].endswith(suffix):
                L[i] = L[i][:-len(suffix)-1]
                break
   ...:             

In [9]: L
Out[9]: 
['I really like',
 "I don't like ham or",
 'Today I ate a lot of',
 'but I also ate a lot of']
请注意,这也将消除目标后缀的任何重复出现:

In [10]: L = ["I really like pineapple pineapple",
"I don't like ham or grapefruit",
"Today I ate a lot of ham",
"but I also ate a lot of grapefruit"]

In [11]: for i in range(len(L)):
    while any(L[i].endswith(suffix) for suffix in suffixes):
        for suffix in suffixes:
            if L[i].endswith(suffix):
                L[i] = L[i][:-len(suffix)-1]
                break
   ....:             

In [12]: L
Out[12]: 
['I really like',
 "I don't like ham or",
 'Today I ate a lot of',
 'but I also ate a lot of']
如果要清除多余的空白,可以执行以下操作:

In [7]: L = ["I really like pineapple",
"I don't like ham or grapefruit",
"Today I ate a lot of ham",
"but I also ate a lot of grapefruit"]

In [8]: for i in range(len(L)):
    while any(L[i].endswith(suffix) for suffix in suffixes):
        for suffix in suffixes:
            if L[i].endswith(suffix):
                L[i] = L[i][:-len(suffix)-1]
                break
   ...:             

In [9]: L
Out[9]: 
['I really like',
 "I don't like ham or",
 'Today I ate a lot of',
 'but I also ate a lot of']
请注意,这也将消除目标后缀的任何重复出现:

In [10]: L = ["I really like pineapple pineapple",
"I don't like ham or grapefruit",
"Today I ate a lot of ham",
"but I also ate a lot of grapefruit"]

In [11]: for i in range(len(L)):
    while any(L[i].endswith(suffix) for suffix in suffixes):
        for suffix in suffixes:
            if L[i].endswith(suffix):
                L[i] = L[i][:-len(suffix)-1]
                break
   ....:             

In [12]: L
Out[12]: 
['I really like',
 "I don't like ham or",
 'Today I ate a lot of',
 'but I also ate a lot of']
如果要清除多余的空白,可以执行以下操作:

In [7]: L = ["I really like pineapple",
"I don't like ham or grapefruit",
"Today I ate a lot of ham",
"but I also ate a lot of grapefruit"]

In [8]: for i in range(len(L)):
    while any(L[i].endswith(suffix) for suffix in suffixes):
        for suffix in suffixes:
            if L[i].endswith(suffix):
                L[i] = L[i][:-len(suffix)-1]
                break
   ...:             

In [9]: L
Out[9]: 
['I really like',
 "I don't like ham or",
 'Today I ate a lot of',
 'but I also ate a lot of']
请注意,这也将消除目标后缀的任何重复出现:

In [10]: L = ["I really like pineapple pineapple",
"I don't like ham or grapefruit",
"Today I ate a lot of ham",
"but I also ate a lot of grapefruit"]

In [11]: for i in range(len(L)):
    while any(L[i].endswith(suffix) for suffix in suffixes):
        for suffix in suffixes:
            if L[i].endswith(suffix):
                L[i] = L[i][:-len(suffix)-1]
                break
   ....:             

In [12]: L
Out[12]: 
['I really like',
 "I don't like ham or",
 'Today I ate a lot of',
 'but I also ate a lot of']
如果要清除多余的空白,可以执行以下操作:

In [7]: L = ["I really like pineapple",
"I don't like ham or grapefruit",
"Today I ate a lot of ham",
"but I also ate a lot of grapefruit"]

In [8]: for i in range(len(L)):
    while any(L[i].endswith(suffix) for suffix in suffixes):
        for suffix in suffixes:
            if L[i].endswith(suffix):
                L[i] = L[i][:-len(suffix)-1]
                break
   ...:             

In [9]: L
Out[9]: 
['I really like',
 "I don't like ham or",
 'Today I ate a lot of',
 'but I also ate a lot of']
请注意,这也将消除目标后缀的任何重复出现:

In [10]: L = ["I really like pineapple pineapple",
"I don't like ham or grapefruit",
"Today I ate a lot of ham",
"but I also ate a lot of grapefruit"]

In [11]: for i in range(len(L)):
    while any(L[i].endswith(suffix) for suffix in suffixes):
        for suffix in suffixes:
            if L[i].endswith(suffix):
                L[i] = L[i][:-len(suffix)-1]
                break
   ....:             

In [12]: L
Out[12]: 
['I really like',
 "I don't like ham or",
 'Today I ate a lot of',
 'but I also ate a lot of']
嗯,我试着用谷歌搜索“rstrip”。我想检查每个字符串是否以“ham”或“菠萝”结尾,但总的来说,有很多单词我想去掉,也有很多单词我不想去掉,我不想写无数的if语句。我试着用谷歌搜索“rstrip”我想检查每个字符串是否以“火腿”或“菠萝”结尾,但总的来说,有很多单词我想去掉,也有很多单词我不想去掉,我不想写无数的if语句。我试着用谷歌搜索“rstrip”。我想检查每个字符串是否以“火腿”或“菠萝”结尾“菠萝”,但总的来说,有很多词我想去掉,也有很多词我不想去掉,我不想写无数的if语句。我试着在谷歌上搜索“rstrip”。我想检查每个字符串是否以“ham”或“菠萝”结尾,“但总的来说,有很多词我想脱掉,也有很多词我不想脱掉,我不想写无数的if语句。