Python:如何删除字符串中的某些单词

时间:2014-11-18 02:17:28

标签: python string

我有以下字符串:

  然后宝贝就在我们离开它的那个岛上   那时你们离开我了

我的任务是删除除了重复出现的单词之外的所有内容。所以输出将是:

>>>right right then left

我设法得到以下输出:

>>>['right', 'then', 'the', 'treasure', 'be', 'on', 'that', 'island', 'where', 'we', 'left', 'it', 'when', 'ye', 'me']

现在,如果我设法从原始字符串中删除上述单词,我将保留正确的输出。我该怎么做?

4 个答案:

答案 0 :(得分:1)

使用Counter

from collections import Counter
words = Counter('right then the treasure be right on that island right where we left it then when ye left me')
for word, count in words.most_common():
    if count > 1:
        print word,

答案 1 :(得分:0)

这将给出所需的输出,打印具有计数>的单词的所有发生的列表。字符串中的1

your_string = 'right then the treasure be right on that island right where we left it     then when ye left me'
words = your_string.split()
words_that_occur_more_than_once = [word for word in words if words.count(word)>1]
words_that_occur_more_than_once.sort(reverse=True)
print(' '.join(words_that_occur_more_than_once))

这将按您希望的方式对字符串进行排序。我不明白为什么输出应该少一个'正确'其中的一句话。

答案 2 :(得分:0)

首先创建一个方法来确定字符串是否出现多次。然后使用该方法提取您想要的单词,然后重新打印。

# Method returns True if the searchWord is in the text only once.
# Returns False otherwise.
def occursOnlyOnce(searchWord, text):
    count = 0
    words = text.split();
    for word in words:
        if word == searchWord:
            count += 1

    if count == 1:
        return True
    else:
        return False


# Initial input text.
initialText = "right then the treasure be right on that island right where we left it then when ye left me"

# Split words into a list.
words = initialText.split();

# List of recurring words only.
finalWords = []

# Extract words that don't appear once, and place into a list.
for word in words:
    if not occursOnlyOnce(word, initialText):
        finalWords.append(word)

# Assemble a space-delimited string containing the words.
finalStr = " ".join(finalWords)

# Print out the words that occur more than once.
print(finalStr)

答案 3 :(得分:0)

此代码将正确删除所有内容并删除多余的空格。

def removePhrase(string, charArray):

    string = str(string)
    text = string.split()

    for x in range (0, len(text)):
        for y in range (0, len(charArray)):
            if text[x] == charArray[y]:
                text[x] = text[x].replace(charArray[y], "")

    text = " ".join(text)
    return text

string = "right then the treasure be right on that island right where we left it then when ye left me"
text = string.split()
charArray = [word for word in text if text.count(word)<2]
print(charArray)
finalString = removePhrase(string, charArray)
finalString = ' '.join(finalString.split())
print(finalString)