Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.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_Replace - Fatal编程技术网

Python 从字符串末尾删除特定单词

Python 从字符串末尾删除特定单词,python,string,replace,Python,String,Replace,我试图删除字符串末尾的特定单词,直到字符串末尾不再有这些单词为止 我尝试了以下方法: companylist=['dell inc corp', 'the co dell corp inc', 'the co dell corp inc co'] def rchop(thestring, ending): if thestring.endswith(ending): return thestring[:-len(ending)] return thestring for it

我试图删除字符串末尾的特定单词,直到字符串末尾不再有这些单词为止

我尝试了以下方法:

companylist=['dell inc corp', 'the co dell corp inc', 'the co dell corp inc co']

def rchop(thestring, ending):
  if thestring.endswith(ending):
    return thestring[:-len(ending)]
  return thestring

for item in companylist:
    item = rchop(item,' co')
    item = rchop(item,' corp')
    item = rchop(item,' inc')
我期待以下结果:

dell
the co dell
the co dell
但我得到的结果是:

dell
the co dell corp
the co dell corp
如何使结果不依赖于替换词的顺序,以便我的结果表示字符串末尾所有替换词的用尽情况?

您应该使用:

companylist = ['dell inc corp', 'co dell corp inc', 'co dell corp inc co']
for idx, item in enumerate(companylist):
    companylist[idx] = item.replace(' co', '')
    companylist[idx] = item.replace(' corp', '')
    companylist[idx] = item.replace(' inc', '')
或者感谢@RoadRunner:

companylist = [item.replace(' co', '').replace(' corp', '').replace(' inc', '') for item in companylist]
现在这两种情况都发生了:

print(companylist)
是:


如果最后一个单词在其他单词列表中,您可以使用此选项删除它:

import re

string = "hello how are you"
words_to_remove = ["are", "you"]

space_positions = [x.start() for x in re.finditer('\ ', string)]
print(space_positions)
for i in reversed(space_positions):
    if string[i+1:] in words_to_remove:
        string = string[:i]

print(string)
哪些产出:

[5, 9, 13]
hello how
[5, 9, 13]
hello how are ---  you?
hello how ---  are you?
hello ---  how are you?
如果您只想删除最后一个单词,无论它是什么,您都可以使用:

import re

string = "hello how are you?"

space_positions = [x.start() for x in re.finditer('\ ', string)]
print(space_positions)
for i in reversed(space_positions):
    print(string[:i], '---', string[i:])
哪些产出:

[5, 9, 13]
hello how
[5, 9, 13]
hello how are ---  you?
hello how ---  are you?
hello ---  how are you?
字符串[:i]部分是第i个空格之前的所有内容,而字符串[i:]部分是第i个空格之后的所有内容。

使用正则表达式

例:

输出:


另一种方法是:

companylist=['dell inc corp', 'co dell corp inc', 'co dell corp inc co']    
repList = [' inc',' corp',' corp inc']   # list of all the chars to replace  

for elem, s in zip(repList, companylist):
    print(s.partition(elem)[0])
输出:

编辑:

使用列表理解:

输出:


什么是rchop?这是你做的一个函数吗?如果您这样做了,请添加代码?在我的编辑中使用orok包含的rchop def编写rchop为什么要在字符串末尾,而您似乎想从其中的任何位置删除单词?根据您的预期输出在我的预期输出中,我没有建议要删除字符串中的任何位置。总之,我修改了这些例子,让它更清楚我想要的是什么图格,你完全误解了这个问题,OP问的是完全不同的问题,所以它不删除任何单词,它删除特定的单词。问题标题可能更好。更新后可以更好地回答问题。此功能非常有效=。对于之前的混乱,很抱歉。您也可以使用[item.replace'co',.replace'corp',.replace'inc',为companylist中的项创建一个新列表。我用更合适的解释更新了我的问题。”当我希望在这个字符串中保留“co”时,co dell corp inc“将成为您示例中的“dell”,因为一旦corp和inc从末尾剥离,字符串中的最后一个单词就不再与我试图剥离的单词匹配。我用更合适的解释更新了我的问题。”在您的示例中,当我希望在这个字符串中保留“co”时,co dell corp inc”将成为“dell”,因为一旦corp和inc从末尾剥离,字符串中的最后一个单词就不再与我试图剥离的单词匹配away@michael即使你更新了公司列表=['dell inc'、'co dell corp'、'co dell corp']它返回['dell'、'co dell'、'co dell']这不是所需的输出吗?
companylist=['dell inc corp', 'co dell corp inc', 'co dell corp inc co']    
repList = [' inc',' corp',' corp inc']   # list of all the chars to replace  

for elem, s in zip(repList, companylist):
    print(s.partition(elem)[0])
dell
co dell
co dell
print([s.partition(elem)[0] for (elem,s) in zip(repList,companylist)])
['dell', 'co dell', 'co dell']