Kee
Kee

Reputation: 163

Alter the letter in list of strings

An example:

eword_list =  ["a", "is", "bus", "on", "the"]
alter_the_list("A bus station is where a bus stops  A train station is where a train stops  On my desk I have a work station", word_list)
print("1.", word_list)

word_list =  ["a", 'up', "you", "it", "on", "the", 'is']
alter_the_list("It is up to YOU", word_list)
print("2.", word_list)

word_list =  ["easy", "come", "go"]
alter_the_list("Easy come easy go go go", word_list)
print("3.", word_list)

word_list =  ["a", "is", "i", "on"]
alter_the_list("", word_list)
print("4.", word_list)

word_list =  ["a", "is", "i", "on", "the"]
alter_the_list("May your coffee be strong and your Monday be short", word_list)
print("5.", word_list)

def alter_the_list(text, word_list):
    return[text for text in word_list if text in word_list]

I'm trying to remove any word from the list of words which is a separate word in the string of text. The string of text should be converted to lower case before I check the elements of the list of words are all in lower case. There is no punctuation in the string of text and each word in the parameter list of word is unique. I don't know how to fix it.

output:

1. ['a', 'is', 'bus', 'on', 'the']
2. ['a', 'up', 'you', 'it', 'on', 'the', 'is']
3. ['easy', 'come', 'go']
4. ['a', 'is', 'i', 'on']
5. ['a', 'is', 'i', 'on', 'the']

expected:

 1. ['the']
 2. ['a', 'on', 'the']
 3. []
 4. ['a', 'is', 'i', 'on']
 5. ['a', 'is', 'i', 'on', 'the']

Upvotes: 0

Views: 74

Answers (4)

James Taylor
James Taylor

Reputation: 6258

I like @Simon's answer better, but if you want to do it in two list comprehensions:

def alter_the_list(text, word_list):
    # Pull out all words found in the word list
    c = [w for w in word_list for t in text.split() if t == w]
    # Find the difference of the two lists
    return [w for w in word_list if w not in c]

Upvotes: 0

mhawke
mhawke

Reputation: 87084

If the order of the resulting list does not matter you can use sets:

def alter_the_list(text, word_list):
    word_list[:] = set(word_list).difference(text.lower().split())

This function will update word_list in place due to the assignment to the list slice with word_list[:] = ...

Upvotes: 1

Prune
Prune

Reputation: 77857

1 Your main problem is that you return a value from your function, but then ignore it. You have to save it in some way to print out, such as:

word_list =  ["easy", "come", "go"]
word_out = alter_the_list("Easy come easy go go go", word_list)
print("3.", word_out)

What you printed is the original word list, not the function result.

2 You ignore the text parameter to the function. You reuse the variable name as a loop index in your list comprehension. Get a different variable name, such as

return[word for word in word_list if word in word_list]

3 You still have to involve text in the logic of the list you build. Remember that you're looking for words that are not in the given text.

Most of all, learn basic debugging. See this lovely debug blog for help.

If nothing else, learn to use simple print statements to display the values of your variables, and to trace program execution.

Does that get you moving toward a solution?

Upvotes: 0

Simon Kuang
Simon Kuang

Reputation: 3940

I've done it like this:

def alter_the_list(text, word_list):
    for word in text.lower().split():
        if word in word_list:
            word_list.remove(word)

text.lower().split() returns a list of all space-separated tokens in text.

The key is that you're required to alter word_list. It is not enough to return a new list; you have to use Python 3's list methods to modify the list in-place.

Upvotes: 1

Related Questions