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

问题描述 投票:2回答:4

我试图从字符串的末尾删除特定的单词,直到字符串末尾不再有任何这些单词。

我尝试了以下方法:

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

如何使结果不依赖于替换单词的顺序,所以我的结果代表了从字符串末尾开始的所有替换单词的耗尽?

python string replace
4个回答
2
投票

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

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

如果您只想删除最后一个单词,无论它是什么,您都可以使用:

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 are ---  you?
hello how ---  are you?
hello ---  how are you?

string[:i]部分是第i个空间之前的所有部分,而string[i:]部分是第i个空间之后的所有部分。


2
投票

使用正则表达式。

例如:

import re

companylist=['dell inc corp', 'co dell corp inc', 'co dell corp inc co']
for i in companylist:
    print(re.sub(r"\W(corp|inc|co)\b", "", i))

输出:

dell
co dell
co dell

0
投票

你应该使用:

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)

方法是:

['dell', 'co dell', 'co dell']

0
投票

完成它的另一种方法:

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

OUTPUT:

dell
co dell
co dell

编辑:

使用list comprehension

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

OUTPUT:

['dell', 'co dell', 'co dell']
© www.soinside.com 2019 - 2024. All rights reserved.