user3036519
user3036519

Reputation: 115

Python: How to remove certain words in a string

I have the following string:

right then the treasure be right on that island right where we left it then when ye left me

My task is to delete everything except the reoccurring words. So the output would be:

>>>right right then left

I have managed to get the following output:

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

Now if i manage to delete the above words from the original string, I will be left the the correct output. How do i do this?

Upvotes: 0

Views: 1595

Answers (4)

Alvaro
Alvaro

Reputation: 12037

This will give the desired output, printing a list of all the ocurrences of the words that have a count > 1 in the string

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

This will order the string the way you want it. I don't understand why the output should have one less 'right' word in it.

Upvotes: 0

Ryan
Ryan

Reputation: 11

First create a method to determine if the string shows up more than once. Then use that method to extract the words you want, and then reprint.

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

Upvotes: 0

Eric Lopushansky
Eric Lopushansky

Reputation: 31

This code will remove everything properly and remove extra spaces.

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)

Upvotes: 0

vikramls
vikramls

Reputation: 1822

Use a 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,

Upvotes: 1

Related Questions