Mapping and Filtering Help for Lists

Z

Zethex

Alright I got asked today by a friend this question, which obviously I
couldn't help him with.

He needs to get rid of words in a string referring to an already given list
then needs to map them using a function he already has. Ill explain this
better by giving an example :p

Say ur given these lists:

un_words = ['a', 'the', 'he', 'she', 'uses', 'with']
alterns = [ ['book', 'textbook', 'notepad'], ['pencil', 'pen', 'pacer'] ]

The problem asks to create a "compareandremove" so that you can use it on a
string, to remove the words from the string that are contained in un_words.

The remaining words then need to be compared to the alterns list and either
bring back the word if no matches or bring back the list. To better explain
that i'll use an example.

If i do compareandremove('notepad a pencil with desk')

I need it so it removes words contained in un_words, so "a" and "with";
then compares the remaining words to alterns to find a match.

This should bring back:

['notepad', 'book', 'textbook', 'pencil', 'pen', 'pacer', 'desk']


Any tips on how to create this function or maybe the function itself so I
can then show him how to do it.

Thank you.
 
A

Arnaud Delobelle

Zethex said:
Alright I got asked today by a friend this question, which obviously I
couldn't help him with.

He needs to get rid of words in a string referring to an already given list
then needs to map them using a function he already has. Ill explain this
better by giving an example :p

Say ur given these lists:

un_words = ['a', 'the', 'he', 'she', 'uses', 'with']
alterns = [ ['book', 'textbook', 'notepad'], ['pencil', 'pen', 'pacer'] ]

The problem asks to create a "compareandremove" so that you can use it on a
string, to remove the words from the string that are contained in un_words.

The remaining words then need to be compared to the alterns list and either
bring back the word if no matches or bring back the list. To better explain
that i'll use an example.

If i do compareandremove('notepad a pencil with desk')

I need it so it removes words contained in un_words, so "a" and "with";
then compares the remaining words to alterns to find a match.

This should bring back:

['notepad', 'book', 'textbook', 'pencil', 'pen', 'pacer', 'desk']


Any tips on how to create this function or maybe the function itself so I
can then show him how to do it.

Thank you.

Look away now if you don't want a complete solution!

un_words = ['a', 'the', 'he', 'she', 'uses', 'with']
alterns = [ ['book', 'textbook', 'notepad'], ['pencil', 'pen', 'pacer'] ]

# these words will be replaced with nothing
wordmap = dict((w, []) for w in un_words)

# these words will be replaced with the list of their synonyms
for wordlist in alterns:
for word in wordlist:
wordmap[word] = wordlist

def compareandremove(sentence):
return [x for w in sentence.split() for x in wordmap.get(w, [w])]


# Example
['book', 'textbook', 'notepad', 'pencil', 'pen', 'pacer', 'desk']
 
Z

Zethex

Thank you a lot!

Another quick couple of questions.

How can i make it so it removes special operators such as ? ! or doesnt
include them?

and

I need to lowercase the words so should i use sentence = sentence.lower()?
 
A

Arnaud Delobelle

Zethex said:
Thank you a lot!

Another quick couple of questions.

How can i make it so it removes special operators such as ? ! or doesnt
include them?

As you are doing lots of string manipulations, I advise you to read
the section of the python documentation on strings (link below). This
way you will get a good idea of what can be done out of the box with
strings in python.

http://docs.python.org/lib/string-methods.html

(Or type help(str) from the interactive prompt)

For the particular task of removing a character, you may be interested
in the replace() method.
and

I need to lowercase the words so should i use sentence = sentence.lower()?

Yes.

Or you could combine the two by using the translate() method. E.g.
'you are free'

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top